1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
|
/*
(c) Copyright 2012-2013 DirectFB integrated media GmbH
(c) Copyright 2001-2013 The world wide DirectFB Open Source Community (directfb.org)
(c) Copyright 2000-2004 Convergence (integrated media) GmbH
All rights reserved.
Written by Denis Oliver Kropp <dok@directfb.org>,
Andreas Shimokawa <andi@directfb.org>,
Marek Pikarski <mass@directfb.org>,
Sven Neumann <neo@directfb.org>,
Ville Syrjälä <syrjala@sci.fi> and
Claudio Ciccani <klan@users.sf.net>.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <asm/types.h> /* Needs to be included before dfb_types.h */
#include <directfb.h>
#include <direct/mem.h>
#include <direct/messages.h>
#include <direct/util.h>
#include <fusion/fusion.h>
#include <fusion/shmalloc.h>
#include <core/core.h>
#include <core/coredefs.h>
#include <core/coretypes.h>
#include <misc/conf.h>
#include "fbdev.h"
#include "agp.h"
#define PAGE_SIZE direct_pagesize()
/*****************************************************************************/
extern FBDev *dfb_fbdev;
static AGPDevice *dfb_agp = NULL;
/*****************************************************************************/
static DFBResult
dfb_agp_info( agp_info *info )
{
D_ASSERT( info != NULL );
if (ioctl( dfb_agp->fd, AGPIOC_INFO, info )) {
D_PERROR( "DirectFB/FBDev/agp: Could not get AGP info!\n" );
return errno2result( errno );
}
return DFB_OK;
}
static DFBResult
dfb_agp_setup( u32 mode )
{
agp_setup setup;
setup.agp_mode = mode;
if (ioctl( dfb_agp->fd, AGPIOC_SETUP, &setup )) {
D_PERROR( "DirectFB/FBDev/agp: AGP setup failed!\n" );
return errno2result( errno );
}
return DFB_OK;
}
static DFBResult
dfb_agp_acquire( void )
{
if (ioctl( dfb_agp->fd, AGPIOC_ACQUIRE, 0 )) {
D_PERROR( "DirectFB/FBDev/agp: Acquire failed!\n" );
return errno2result( errno );
}
return DFB_OK;
}
static DFBResult
dfb_agp_release( void )
{
if (ioctl( dfb_agp->fd, AGPIOC_RELEASE, 0 )) {
D_PERROR( "DirectFB/FBDev/agp: Release failed!\n" );
return errno2result( errno );
}
return DFB_OK;
}
static DFBResult
dfb_agp_allocate( unsigned long size, int *key )
{
agp_allocate alloc;
int pages;
D_ASSERT( key != NULL );
pages = size / PAGE_SIZE;
if (pages % PAGE_SIZE)
pages++;
if (pages == 0) {
D_BUG( "attempted to allocate 0 pages!");
return DFB_BUG;
}
alloc.pg_count = pages;
alloc.type = 0;
if (ioctl( dfb_agp->fd, AGPIOC_ALLOCATE, &alloc )) {
D_PERROR( "DirectFB/FBDev/agp: "
"Could not allocate %d pages!\n", pages );
return errno2result( errno );
}
*key = alloc.key;
return DFB_OK;
}
static DFBResult
dfb_agp_deallocate( int key )
{
if (ioctl( dfb_agp->fd, AGPIOC_DEALLOCATE, key )) {
D_PERROR( "DirectFB/FBDev/agp: "
"Deallocate failed (key = %d)!\n", key );
return errno2result( errno );
}
return DFB_OK;
}
static DFBResult
dfb_agp_bind( unsigned int offset, int key )
{
agp_bind bind;
if (offset % PAGE_SIZE) {
D_BUG( "offset is not page-aligned!" );
return DFB_BUG;
}
bind.pg_start = offset / PAGE_SIZE;
bind.key = key;
if (ioctl( dfb_agp->fd, AGPIOC_BIND, &bind )) {
D_PERROR( "DirectFB/FBDev/agp: "
"Bind failed (key = %d, offset = 0x%x)!\n",
key, offset );
return errno2result( errno );
}
return DFB_OK;
}
static DFBResult
dfb_agp_unbind( int key )
{
agp_unbind unbind;
unbind.priority = 0;
unbind.key = key;
if (ioctl( dfb_agp->fd, AGPIOC_UNBIND, &unbind )) {
D_PERROR( "DirectFB/FBDev/agp: "
"Unbind failed (key = %d)!\n",
key );
return errno2result( errno );
}
return DFB_OK;
}
/*****************************************************************************/
static inline u16
pci_read_word( int fd, int pos )
{
u8 b[2];
if (pread( fd, b, 2, pos ) < 2)
return 0;
return b[0] | (b[1] << 8);
}
static inline u8
pci_read_byte( int fd, int pos )
{
u8 b;
if (pread( fd, &b, 1, pos ) < 1)
return 0;
return b;
}
#define PCI_STATUS 0x06
#define PCI_STATUS_CAP_LIST 0x10
#define PCI_CAPABILITY_LIST 0x34
#define PCI_CAP_ID_AGP 0x02
static bool
dfb_agp_capable( int bus, int dev, int func )
{
bool found = false;
char path[22];
int fd;
/* XXX: the following detection method requires suid root */
snprintf( path, sizeof(path),
"/proc/bus/pci/%02x/%02x.%01x", bus, dev, func );
fd = open( path, O_RDONLY | O_SYNC );
if (fd < 0) {
D_PERROR( "DirectFB/FBDev/agp: "
"Couldn't open '%s'!\n", path );
return false;
}
/* stolen from linux/drivers/pci/pci.c */
if (pci_read_word( fd, PCI_STATUS ) & PCI_STATUS_CAP_LIST) {
int pos, id;
int ttl = 48;
pos = pci_read_byte( fd, PCI_CAPABILITY_LIST );
while (ttl-- && pos >= 0x40) {
pos &= ~3;
id = pci_read_byte( fd, pos );
if (id == 0xff)
break;
if (id == PCI_CAP_ID_AGP) {
found = true;
break;
}
pos = pci_read_byte( fd, pos+1 );
}
}
close( fd );
return found;
}
/*****************************************************************************/
DFBResult
dfb_agp_initialize( void )
{
AGPShared *shared;
unsigned int agp_avail;
DFBResult ret = DFB_FAILURE;
if (dfb_agp) {
D_BUG( "dfb_agp_initialize() already called!" );
return DFB_BUG;
}
/* Precheck for AGP capable device. */
if (!dfb_agp_capable( dfb_fbdev->shared->pci.bus,
dfb_fbdev->shared->pci.dev,
dfb_fbdev->shared->pci.func ))
return DFB_UNSUPPORTED;
dfb_agp = D_CALLOC( 1, sizeof(AGPDevice) );
if (!dfb_agp)
return D_OOM();
shared = SHCALLOC( dfb_fbdev->shared->shmpool, 1, sizeof(AGPShared) );
if (!shared) {
D_ERROR( "DirectFB/FBDev/agp: Could not allocate shared memory!\n" );
ret = DFB_NOSHAREDMEMORY;
goto error0;
}
dfb_agp->fd = direct_try_open( "/dev/agpgart",
"/dev/misc/agpgart", O_RDWR, true );
if (dfb_agp->fd < 0) {
ret = errno2result( errno );
D_ERROR( "DirectFB/FBDev/agp: Error opening AGP device!\n" );
goto error1;
}
ret = dfb_agp_acquire();
if (ret)
goto error2;
ret = dfb_agp_info( &shared->info );
if (ret)
goto error2;
D_DEBUG( "DirectFB/FBDev/agp: "
"Bridge supports: AGP%s%s%s%s%s%s\n",
shared->info.agp_mode & 0x001 ? " 1X" : "",
shared->info.agp_mode & 0x002 ? " 2X" : "",
shared->info.agp_mode & 0x004 ? " 4X" : "",
shared->info.agp_mode & 0x008 ? " 8X" : "",
shared->info.agp_mode & 0x200 ? ", SBA" : "",
shared->info.agp_mode & 0x010 ? ", FW" : "" );
shared->info.agp_mode &= ~0xf;
shared->info.agp_mode |= dfb_config->agp;
shared->info.agp_mode |= dfb_config->agp - 1;
ret = dfb_agp_setup( shared->info.agp_mode );
if (ret)
goto error2;
dfb_agp_info( &shared->info );
D_DEBUG( "DirectFB/FBDev/agp: "
"AGP aperture at 0x%x (%zu MB)\n",
(unsigned int)shared->info.aper_base, shared->info.aper_size );
agp_avail = (shared->info.pg_total - shared->info.pg_used) * PAGE_SIZE;
if (agp_avail == 0) {
D_ERROR( "DirectFB/FBDev/agp: No AGP memory available!\n" );
ret = DFB_INIT;
goto error2;
}
shared->agp_mem = shared->info.aper_size << 20;
if (shared->agp_mem > agp_avail)
shared->agp_mem = agp_avail;
ret = dfb_agp_allocate( shared->agp_mem, &shared->agp_key );
if (ret)
goto error3;
ret = dfb_agp_bind( shared->agp_key, 0 );
if (ret)
goto error4;
dfb_agp->base = mmap( NULL, shared->info.aper_size << 20,
PROT_READ | PROT_WRITE, MAP_SHARED,
dfb_agp->fd, 0 );
if (dfb_agp->base == MAP_FAILED) {
D_PERROR( "DirectFB/FBDev/agp: Could not mmap the AGP aperture!\n" );
ret = DFB_INIT;
goto error5;
}
dfb_agp_release();
dfb_fbdev->agp = dfb_agp;
dfb_fbdev->shared->agp = shared;
return DFB_OK;
error5:
dfb_agp_unbind( shared->agp_key );
error4:
dfb_agp_deallocate( shared->agp_key );
error3:
dfb_agp_release();
error2:
close( dfb_agp->fd );
error1:
SHFREE( dfb_fbdev->shared->shmpool, shared );
error0:
D_FREE( dfb_agp );
dfb_agp = NULL;
return ret;
}
DFBResult
dfb_agp_join( void )
{
AGPShared *shared;
DFBResult ret = DFB_FAILURE;
if (dfb_agp) {
D_BUG( "dfb_agp_join() already called!" );
return DFB_BUG;
}
shared = dfb_fbdev->shared->agp;
if (!shared)
return DFB_OK;
dfb_agp = D_CALLOC( 1, sizeof(AGPDevice) );
if (!dfb_agp)
return D_OOM();
dfb_agp->fd = direct_try_open( "/dev/agpgart",
"/dev/misc/agpgart", O_RDWR, true );
if (dfb_agp->fd < 0) {
ret = errno2result( errno );
D_ERROR( "DirectFB/FBDev/agp: Error opening AGP device!\n" );
goto error0;
}
ret = dfb_agp_acquire();
if (ret)
goto error1;
dfb_agp->base = mmap( NULL, shared->info.aper_size << 20,
PROT_READ | PROT_WRITE, MAP_SHARED,
dfb_agp->fd, 0 );
if (dfb_agp->base == MAP_FAILED) {
D_PERROR( "DirectFB/FBDev/agp: Could not mmap the AGP aperture!\n" );
ret = DFB_INIT;
goto error2;
}
D_DEBUG( "DirectFB/FBDev/agp: AGP aperture mapped at %p\n", dfb_agp->base );
dfb_agp_release();
dfb_fbdev->agp = dfb_agp;
return DFB_OK;
error2:
dfb_agp_release();
error1:
close( dfb_agp->fd );
error0:
D_FREE( dfb_agp );
dfb_agp = NULL;
return ret;
}
DFBResult
dfb_agp_shutdown( void )
{
AGPShared *shared;
if (!dfb_agp)
return DFB_INVARG;
shared = dfb_fbdev->shared->agp;
dfb_agp_acquire();
munmap( dfb_agp->base, shared->info.aper_size << 20 );
dfb_agp_unbind( shared->agp_key );
dfb_agp_deallocate( shared->agp_key );
dfb_agp_release();
close( dfb_agp->fd );
SHFREE( dfb_fbdev->shared->shmpool, shared );
D_FREE( dfb_agp );
dfb_fbdev->shared->agp = NULL;
dfb_fbdev->agp = dfb_agp = NULL;
return DFB_OK;
}
DFBResult
dfb_agp_leave( void )
{
AGPShared *shared;
if (!dfb_agp)
return DFB_INVARG;
shared = dfb_fbdev->shared->agp;
dfb_agp_acquire();
munmap( dfb_agp->base, shared->info.aper_size << 20 );
dfb_agp_release();
close( dfb_agp->fd );
D_FREE( dfb_agp );
dfb_fbdev->agp = dfb_agp = NULL;
return DFB_OK;
}
|