generic.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207
  1. /*
  2. * AGPGART driver.
  3. * Copyright (C) 2004 Silicon Graphics, Inc.
  4. * Copyright (C) 2002-2005 Dave Jones.
  5. * Copyright (C) 1999 Jeff Hartmann.
  6. * Copyright (C) 1999 Precision Insight, Inc.
  7. * Copyright (C) 1999 Xi Graphics, Inc.
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a
  10. * copy of this software and associated documentation files (the "Software"),
  11. * to deal in the Software without restriction, including without limitation
  12. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  13. * and/or sell copies of the Software, and to permit persons to whom the
  14. * Software is furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included
  17. * in all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  20. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  22. * JEFF HARTMANN, OR ANY OTHER CONTRIBUTORS BE LIABLE FOR ANY CLAIM,
  23. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  24. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  25. * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. *
  27. * TODO:
  28. * - Allocate more than order 0 pages to avoid too much linear map splitting.
  29. */
  30. #include <linux/module.h>
  31. #include <linux/pci.h>
  32. #include <linux/init.h>
  33. #include <linux/pagemap.h>
  34. #include <linux/miscdevice.h>
  35. #include <linux/pm.h>
  36. #include <linux/agp_backend.h>
  37. #include <linux/vmalloc.h>
  38. #include <linux/dma-mapping.h>
  39. #include <linux/mm.h>
  40. #include <asm/io.h>
  41. #include <asm/cacheflush.h>
  42. #include <asm/pgtable.h>
  43. #include "agp.h"
  44. __u32 *agp_gatt_table;
  45. int agp_memory_reserved;
  46. /*
  47. * Needed by the Nforce GART driver for the time being. Would be
  48. * nice to do this some other way instead of needing this export.
  49. */
  50. EXPORT_SYMBOL_GPL(agp_memory_reserved);
  51. #if defined(CONFIG_X86)
  52. int map_page_into_agp(struct page *page)
  53. {
  54. int i;
  55. i = change_page_attr(page, 1, PAGE_KERNEL_NOCACHE);
  56. /* Caller's responsibility to call global_flush_tlb() for
  57. * performance reasons */
  58. return i;
  59. }
  60. EXPORT_SYMBOL_GPL(map_page_into_agp);
  61. int unmap_page_from_agp(struct page *page)
  62. {
  63. int i;
  64. i = change_page_attr(page, 1, PAGE_KERNEL);
  65. /* Caller's responsibility to call global_flush_tlb() for
  66. * performance reasons */
  67. return i;
  68. }
  69. EXPORT_SYMBOL_GPL(unmap_page_from_agp);
  70. #endif
  71. /*
  72. * Generic routines for handling agp_memory structures -
  73. * They use the basic page allocation routines to do the brunt of the work.
  74. */
  75. void agp_free_key(int key)
  76. {
  77. if (key < 0)
  78. return;
  79. if (key < MAXKEY)
  80. clear_bit(key, agp_bridge->key_list);
  81. }
  82. EXPORT_SYMBOL(agp_free_key);
  83. static int agp_get_key(void)
  84. {
  85. int bit;
  86. bit = find_first_zero_bit(agp_bridge->key_list, MAXKEY);
  87. if (bit < MAXKEY) {
  88. set_bit(bit, agp_bridge->key_list);
  89. return bit;
  90. }
  91. return -1;
  92. }
  93. struct agp_memory *agp_create_memory(int scratch_pages)
  94. {
  95. struct agp_memory *new;
  96. new = kzalloc(sizeof(struct agp_memory), GFP_KERNEL);
  97. if (new == NULL)
  98. return NULL;
  99. new->key = agp_get_key();
  100. if (new->key < 0) {
  101. kfree(new);
  102. return NULL;
  103. }
  104. new->memory = vmalloc(PAGE_SIZE * scratch_pages);
  105. if (new->memory == NULL) {
  106. agp_free_key(new->key);
  107. kfree(new);
  108. return NULL;
  109. }
  110. new->num_scratch_pages = scratch_pages;
  111. return new;
  112. }
  113. EXPORT_SYMBOL(agp_create_memory);
  114. /**
  115. * agp_free_memory - free memory associated with an agp_memory pointer.
  116. *
  117. * @curr: agp_memory pointer to be freed.
  118. *
  119. * It is the only function that can be called when the backend is not owned
  120. * by the caller. (So it can free memory on client death.)
  121. */
  122. void agp_free_memory(struct agp_memory *curr)
  123. {
  124. size_t i;
  125. if (curr == NULL)
  126. return;
  127. if (curr->is_bound == TRUE)
  128. agp_unbind_memory(curr);
  129. if (curr->type != 0) {
  130. curr->bridge->driver->free_by_type(curr);
  131. return;
  132. }
  133. if (curr->page_count != 0) {
  134. for (i = 0; i < curr->page_count; i++) {
  135. curr->bridge->driver->agp_destroy_page(gart_to_virt(curr->memory[i]));
  136. }
  137. flush_agp_mappings();
  138. }
  139. agp_free_key(curr->key);
  140. vfree(curr->memory);
  141. kfree(curr);
  142. }
  143. EXPORT_SYMBOL(agp_free_memory);
  144. #define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(unsigned long))
  145. /**
  146. * agp_allocate_memory - allocate a group of pages of a certain type.
  147. *
  148. * @page_count: size_t argument of the number of pages
  149. * @type: u32 argument of the type of memory to be allocated.
  150. *
  151. * Every agp bridge device will allow you to allocate AGP_NORMAL_MEMORY which
  152. * maps to physical ram. Any other type is device dependent.
  153. *
  154. * It returns NULL whenever memory is unavailable.
  155. */
  156. struct agp_memory *agp_allocate_memory(struct agp_bridge_data *bridge,
  157. size_t page_count, u32 type)
  158. {
  159. int scratch_pages;
  160. struct agp_memory *new;
  161. size_t i;
  162. if (!bridge)
  163. return NULL;
  164. if ((atomic_read(&bridge->current_memory_agp) + page_count) > bridge->max_memory_agp)
  165. return NULL;
  166. if (type != 0) {
  167. new = bridge->driver->alloc_by_type(page_count, type);
  168. if (new)
  169. new->bridge = bridge;
  170. return new;
  171. }
  172. scratch_pages = (page_count + ENTRIES_PER_PAGE - 1) / ENTRIES_PER_PAGE;
  173. new = agp_create_memory(scratch_pages);
  174. if (new == NULL)
  175. return NULL;
  176. for (i = 0; i < page_count; i++) {
  177. void *addr = bridge->driver->agp_alloc_page(bridge);
  178. if (addr == NULL) {
  179. agp_free_memory(new);
  180. return NULL;
  181. }
  182. new->memory[i] = virt_to_gart(addr);
  183. new->page_count++;
  184. }
  185. new->bridge = bridge;
  186. flush_agp_mappings();
  187. return new;
  188. }
  189. EXPORT_SYMBOL(agp_allocate_memory);
  190. /* End - Generic routines for handling agp_memory structures */
  191. static int agp_return_size(void)
  192. {
  193. int current_size;
  194. void *temp;
  195. temp = agp_bridge->current_size;
  196. switch (agp_bridge->driver->size_type) {
  197. case U8_APER_SIZE:
  198. current_size = A_SIZE_8(temp)->size;
  199. break;
  200. case U16_APER_SIZE:
  201. current_size = A_SIZE_16(temp)->size;
  202. break;
  203. case U32_APER_SIZE:
  204. current_size = A_SIZE_32(temp)->size;
  205. break;
  206. case LVL2_APER_SIZE:
  207. current_size = A_SIZE_LVL2(temp)->size;
  208. break;
  209. case FIXED_APER_SIZE:
  210. current_size = A_SIZE_FIX(temp)->size;
  211. break;
  212. default:
  213. current_size = 0;
  214. break;
  215. }
  216. current_size -= (agp_memory_reserved / (1024*1024));
  217. if (current_size <0)
  218. current_size = 0;
  219. return current_size;
  220. }
  221. int agp_num_entries(void)
  222. {
  223. int num_entries;
  224. void *temp;
  225. temp = agp_bridge->current_size;
  226. switch (agp_bridge->driver->size_type) {
  227. case U8_APER_SIZE:
  228. num_entries = A_SIZE_8(temp)->num_entries;
  229. break;
  230. case U16_APER_SIZE:
  231. num_entries = A_SIZE_16(temp)->num_entries;
  232. break;
  233. case U32_APER_SIZE:
  234. num_entries = A_SIZE_32(temp)->num_entries;
  235. break;
  236. case LVL2_APER_SIZE:
  237. num_entries = A_SIZE_LVL2(temp)->num_entries;
  238. break;
  239. case FIXED_APER_SIZE:
  240. num_entries = A_SIZE_FIX(temp)->num_entries;
  241. break;
  242. default:
  243. num_entries = 0;
  244. break;
  245. }
  246. num_entries -= agp_memory_reserved>>PAGE_SHIFT;
  247. if (num_entries<0)
  248. num_entries = 0;
  249. return num_entries;
  250. }
  251. EXPORT_SYMBOL_GPL(agp_num_entries);
  252. /**
  253. * agp_copy_info - copy bridge state information
  254. *
  255. * @info: agp_kern_info pointer. The caller should insure that this pointer is valid.
  256. *
  257. * This function copies information about the agp bridge device and the state of
  258. * the agp backend into an agp_kern_info pointer.
  259. */
  260. int agp_copy_info(struct agp_bridge_data *bridge, struct agp_kern_info *info)
  261. {
  262. memset(info, 0, sizeof(struct agp_kern_info));
  263. if (!bridge) {
  264. info->chipset = NOT_SUPPORTED;
  265. return -EIO;
  266. }
  267. info->version.major = bridge->version->major;
  268. info->version.minor = bridge->version->minor;
  269. info->chipset = SUPPORTED;
  270. info->device = bridge->dev;
  271. if (bridge->mode & AGPSTAT_MODE_3_0)
  272. info->mode = bridge->mode & ~AGP3_RESERVED_MASK;
  273. else
  274. info->mode = bridge->mode & ~AGP2_RESERVED_MASK;
  275. info->aper_base = bridge->gart_bus_addr;
  276. info->aper_size = agp_return_size();
  277. info->max_memory = bridge->max_memory_agp;
  278. info->current_memory = atomic_read(&bridge->current_memory_agp);
  279. info->cant_use_aperture = bridge->driver->cant_use_aperture;
  280. info->vm_ops = bridge->vm_ops;
  281. info->page_mask = ~0UL;
  282. return 0;
  283. }
  284. EXPORT_SYMBOL(agp_copy_info);
  285. /* End - Routine to copy over information structure */
  286. /*
  287. * Routines for handling swapping of agp_memory into the GATT -
  288. * These routines take agp_memory and insert them into the GATT.
  289. * They call device specific routines to actually write to the GATT.
  290. */
  291. /**
  292. * agp_bind_memory - Bind an agp_memory structure into the GATT.
  293. *
  294. * @curr: agp_memory pointer
  295. * @pg_start: an offset into the graphics aperture translation table
  296. *
  297. * It returns -EINVAL if the pointer == NULL.
  298. * It returns -EBUSY if the area of the table requested is already in use.
  299. */
  300. int agp_bind_memory(struct agp_memory *curr, off_t pg_start)
  301. {
  302. int ret_val;
  303. if (curr == NULL)
  304. return -EINVAL;
  305. if (curr->is_bound == TRUE) {
  306. printk(KERN_INFO PFX "memory %p is already bound!\n", curr);
  307. return -EINVAL;
  308. }
  309. if (curr->is_flushed == FALSE) {
  310. curr->bridge->driver->cache_flush();
  311. curr->is_flushed = TRUE;
  312. }
  313. ret_val = curr->bridge->driver->insert_memory(curr, pg_start, curr->type);
  314. if (ret_val != 0)
  315. return ret_val;
  316. curr->is_bound = TRUE;
  317. curr->pg_start = pg_start;
  318. return 0;
  319. }
  320. EXPORT_SYMBOL(agp_bind_memory);
  321. /**
  322. * agp_unbind_memory - Removes an agp_memory structure from the GATT
  323. *
  324. * @curr: agp_memory pointer to be removed from the GATT.
  325. *
  326. * It returns -EINVAL if this piece of agp_memory is not currently bound to
  327. * the graphics aperture translation table or if the agp_memory pointer == NULL
  328. */
  329. int agp_unbind_memory(struct agp_memory *curr)
  330. {
  331. int ret_val;
  332. if (curr == NULL)
  333. return -EINVAL;
  334. if (curr->is_bound != TRUE) {
  335. printk(KERN_INFO PFX "memory %p was not bound!\n", curr);
  336. return -EINVAL;
  337. }
  338. ret_val = curr->bridge->driver->remove_memory(curr, curr->pg_start, curr->type);
  339. if (ret_val != 0)
  340. return ret_val;
  341. curr->is_bound = FALSE;
  342. curr->pg_start = 0;
  343. return 0;
  344. }
  345. EXPORT_SYMBOL(agp_unbind_memory);
  346. /* End - Routines for handling swapping of agp_memory into the GATT */
  347. /* Generic Agp routines - Start */
  348. static void agp_v2_parse_one(u32 *requested_mode, u32 *bridge_agpstat, u32 *vga_agpstat)
  349. {
  350. u32 tmp;
  351. if (*requested_mode & AGP2_RESERVED_MASK) {
  352. printk(KERN_INFO PFX "reserved bits set (%x) in mode 0x%x. Fixed.\n",
  353. *requested_mode & AGP2_RESERVED_MASK, *requested_mode);
  354. *requested_mode &= ~AGP2_RESERVED_MASK;
  355. }
  356. /* Check the speed bits make sense. Only one should be set. */
  357. tmp = *requested_mode & 7;
  358. switch (tmp) {
  359. case 0:
  360. printk(KERN_INFO PFX "%s tried to set rate=x0. Setting to x1 mode.\n", current->comm);
  361. *requested_mode |= AGPSTAT2_1X;
  362. break;
  363. case 1:
  364. case 2:
  365. break;
  366. case 3:
  367. *requested_mode &= ~(AGPSTAT2_1X); /* rate=2 */
  368. break;
  369. case 4:
  370. break;
  371. case 5:
  372. case 6:
  373. case 7:
  374. *requested_mode &= ~(AGPSTAT2_1X|AGPSTAT2_2X); /* rate=4*/
  375. break;
  376. }
  377. /* disable SBA if it's not supported */
  378. if (!((*bridge_agpstat & AGPSTAT_SBA) && (*vga_agpstat & AGPSTAT_SBA) && (*requested_mode & AGPSTAT_SBA)))
  379. *bridge_agpstat &= ~AGPSTAT_SBA;
  380. /* Set rate */
  381. if (!((*bridge_agpstat & AGPSTAT2_4X) && (*vga_agpstat & AGPSTAT2_4X) && (*requested_mode & AGPSTAT2_4X)))
  382. *bridge_agpstat &= ~AGPSTAT2_4X;
  383. if (!((*bridge_agpstat & AGPSTAT2_2X) && (*vga_agpstat & AGPSTAT2_2X) && (*requested_mode & AGPSTAT2_2X)))
  384. *bridge_agpstat &= ~AGPSTAT2_2X;
  385. if (!((*bridge_agpstat & AGPSTAT2_1X) && (*vga_agpstat & AGPSTAT2_1X) && (*requested_mode & AGPSTAT2_1X)))
  386. *bridge_agpstat &= ~AGPSTAT2_1X;
  387. /* Now we know what mode it should be, clear out the unwanted bits. */
  388. if (*bridge_agpstat & AGPSTAT2_4X)
  389. *bridge_agpstat &= ~(AGPSTAT2_1X | AGPSTAT2_2X); /* 4X */
  390. if (*bridge_agpstat & AGPSTAT2_2X)
  391. *bridge_agpstat &= ~(AGPSTAT2_1X | AGPSTAT2_4X); /* 2X */
  392. if (*bridge_agpstat & AGPSTAT2_1X)
  393. *bridge_agpstat &= ~(AGPSTAT2_2X | AGPSTAT2_4X); /* 1X */
  394. /* Apply any errata. */
  395. if (agp_bridge->flags & AGP_ERRATA_FASTWRITES)
  396. *bridge_agpstat &= ~AGPSTAT_FW;
  397. if (agp_bridge->flags & AGP_ERRATA_SBA)
  398. *bridge_agpstat &= ~AGPSTAT_SBA;
  399. if (agp_bridge->flags & AGP_ERRATA_1X) {
  400. *bridge_agpstat &= ~(AGPSTAT2_2X | AGPSTAT2_4X);
  401. *bridge_agpstat |= AGPSTAT2_1X;
  402. }
  403. /* If we've dropped down to 1X, disable fast writes. */
  404. if (*bridge_agpstat & AGPSTAT2_1X)
  405. *bridge_agpstat &= ~AGPSTAT_FW;
  406. }
  407. /*
  408. * requested_mode = Mode requested by (typically) X.
  409. * bridge_agpstat = PCI_AGP_STATUS from agp bridge.
  410. * vga_agpstat = PCI_AGP_STATUS from graphic card.
  411. */
  412. static void agp_v3_parse_one(u32 *requested_mode, u32 *bridge_agpstat, u32 *vga_agpstat)
  413. {
  414. u32 origbridge=*bridge_agpstat, origvga=*vga_agpstat;
  415. u32 tmp;
  416. if (*requested_mode & AGP3_RESERVED_MASK) {
  417. printk(KERN_INFO PFX "reserved bits set (%x) in mode 0x%x. Fixed.\n",
  418. *requested_mode & AGP3_RESERVED_MASK, *requested_mode);
  419. *requested_mode &= ~AGP3_RESERVED_MASK;
  420. }
  421. /* Check the speed bits make sense. */
  422. tmp = *requested_mode & 7;
  423. if (tmp == 0) {
  424. printk(KERN_INFO PFX "%s tried to set rate=x0. Setting to AGP3 x4 mode.\n", current->comm);
  425. *requested_mode |= AGPSTAT3_4X;
  426. }
  427. if (tmp >= 3) {
  428. printk(KERN_INFO PFX "%s tried to set rate=x%d. Setting to AGP3 x8 mode.\n", current->comm, tmp * 4);
  429. *requested_mode = (*requested_mode & ~7) | AGPSTAT3_8X;
  430. }
  431. /* ARQSZ - Set the value to the maximum one.
  432. * Don't allow the mode register to override values. */
  433. *bridge_agpstat = ((*bridge_agpstat & ~AGPSTAT_ARQSZ) |
  434. max_t(u32,(*bridge_agpstat & AGPSTAT_ARQSZ),(*vga_agpstat & AGPSTAT_ARQSZ)));
  435. /* Calibration cycle.
  436. * Don't allow the mode register to override values. */
  437. *bridge_agpstat = ((*bridge_agpstat & ~AGPSTAT_CAL_MASK) |
  438. min_t(u32,(*bridge_agpstat & AGPSTAT_CAL_MASK),(*vga_agpstat & AGPSTAT_CAL_MASK)));
  439. /* SBA *must* be supported for AGP v3 */
  440. *bridge_agpstat |= AGPSTAT_SBA;
  441. /*
  442. * Set speed.
  443. * Check for invalid speeds. This can happen when applications
  444. * written before the AGP 3.0 standard pass AGP2.x modes to AGP3 hardware
  445. */
  446. if (*requested_mode & AGPSTAT_MODE_3_0) {
  447. /*
  448. * Caller hasn't a clue what it is doing. Bridge is in 3.0 mode,
  449. * have been passed a 3.0 mode, but with 2.x speed bits set.
  450. * AGP2.x 4x -> AGP3.0 4x.
  451. */
  452. if (*requested_mode & AGPSTAT2_4X) {
  453. printk(KERN_INFO PFX "%s passes broken AGP3 flags (%x). Fixed.\n",
  454. current->comm, *requested_mode);
  455. *requested_mode &= ~AGPSTAT2_4X;
  456. *requested_mode |= AGPSTAT3_4X;
  457. }
  458. } else {
  459. /*
  460. * The caller doesn't know what they are doing. We are in 3.0 mode,
  461. * but have been passed an AGP 2.x mode.
  462. * Convert AGP 1x,2x,4x -> AGP 3.0 4x.
  463. */
  464. printk(KERN_INFO PFX "%s passes broken AGP2 flags (%x) in AGP3 mode. Fixed.\n",
  465. current->comm, *requested_mode);
  466. *requested_mode &= ~(AGPSTAT2_4X | AGPSTAT2_2X | AGPSTAT2_1X);
  467. *requested_mode |= AGPSTAT3_4X;
  468. }
  469. if (*requested_mode & AGPSTAT3_8X) {
  470. if (!(*bridge_agpstat & AGPSTAT3_8X)) {
  471. *bridge_agpstat &= ~(AGPSTAT3_8X | AGPSTAT3_RSVD);
  472. *bridge_agpstat |= AGPSTAT3_4X;
  473. printk(KERN_INFO PFX "%s requested AGPx8 but bridge not capable.\n", current->comm);
  474. return;
  475. }
  476. if (!(*vga_agpstat & AGPSTAT3_8X)) {
  477. *bridge_agpstat &= ~(AGPSTAT3_8X | AGPSTAT3_RSVD);
  478. *bridge_agpstat |= AGPSTAT3_4X;
  479. printk(KERN_INFO PFX "%s requested AGPx8 but graphic card not capable.\n", current->comm);
  480. return;
  481. }
  482. /* All set, bridge & device can do AGP x8*/
  483. *bridge_agpstat &= ~(AGPSTAT3_4X | AGPSTAT3_RSVD);
  484. goto done;
  485. } else {
  486. /*
  487. * If we didn't specify AGPx8, we can only do x4.
  488. * If the hardware can't do x4, we're up shit creek, and never
  489. * should have got this far.
  490. */
  491. *bridge_agpstat &= ~(AGPSTAT3_8X | AGPSTAT3_RSVD);
  492. if ((*bridge_agpstat & AGPSTAT3_4X) && (*vga_agpstat & AGPSTAT3_4X))
  493. *bridge_agpstat |= AGPSTAT3_4X;
  494. else {
  495. printk(KERN_INFO PFX "Badness. Don't know which AGP mode to set. "
  496. "[bridge_agpstat:%x vga_agpstat:%x fell back to:- bridge_agpstat:%x vga_agpstat:%x]\n",
  497. origbridge, origvga, *bridge_agpstat, *vga_agpstat);
  498. if (!(*bridge_agpstat & AGPSTAT3_4X))
  499. printk(KERN_INFO PFX "Bridge couldn't do AGP x4.\n");
  500. if (!(*vga_agpstat & AGPSTAT3_4X))
  501. printk(KERN_INFO PFX "Graphic card couldn't do AGP x4.\n");
  502. return;
  503. }
  504. }
  505. done:
  506. /* Apply any errata. */
  507. if (agp_bridge->flags & AGP_ERRATA_FASTWRITES)
  508. *bridge_agpstat &= ~AGPSTAT_FW;
  509. if (agp_bridge->flags & AGP_ERRATA_SBA)
  510. *bridge_agpstat &= ~AGPSTAT_SBA;
  511. if (agp_bridge->flags & AGP_ERRATA_1X) {
  512. *bridge_agpstat &= ~(AGPSTAT2_2X | AGPSTAT2_4X);
  513. *bridge_agpstat |= AGPSTAT2_1X;
  514. }
  515. }
  516. /**
  517. * agp_collect_device_status - determine correct agp_cmd from various agp_stat's
  518. * @bridge: an agp_bridge_data struct allocated for the AGP host bridge.
  519. * @requested_mode: requested agp_stat from userspace (Typically from X)
  520. * @bridge_agpstat: current agp_stat from AGP bridge.
  521. *
  522. * This function will hunt for an AGP graphics card, and try to match
  523. * the requested mode to the capabilities of both the bridge and the card.
  524. */
  525. u32 agp_collect_device_status(struct agp_bridge_data *bridge, u32 requested_mode, u32 bridge_agpstat)
  526. {
  527. struct pci_dev *device = NULL;
  528. u32 vga_agpstat;
  529. u8 cap_ptr;
  530. for (;;) {
  531. device = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, device);
  532. if (!device) {
  533. printk(KERN_INFO PFX "Couldn't find an AGP VGA controller.\n");
  534. return 0;
  535. }
  536. cap_ptr = pci_find_capability(device, PCI_CAP_ID_AGP);
  537. if (cap_ptr)
  538. break;
  539. }
  540. /*
  541. * Ok, here we have a AGP device. Disable impossible
  542. * settings, and adjust the readqueue to the minimum.
  543. */
  544. pci_read_config_dword(device, cap_ptr+PCI_AGP_STATUS, &vga_agpstat);
  545. /* adjust RQ depth */
  546. bridge_agpstat = ((bridge_agpstat & ~AGPSTAT_RQ_DEPTH) |
  547. min_t(u32, (requested_mode & AGPSTAT_RQ_DEPTH),
  548. min_t(u32, (bridge_agpstat & AGPSTAT_RQ_DEPTH), (vga_agpstat & AGPSTAT_RQ_DEPTH))));
  549. /* disable FW if it's not supported */
  550. if (!((bridge_agpstat & AGPSTAT_FW) &&
  551. (vga_agpstat & AGPSTAT_FW) &&
  552. (requested_mode & AGPSTAT_FW)))
  553. bridge_agpstat &= ~AGPSTAT_FW;
  554. /* Check to see if we are operating in 3.0 mode */
  555. if (agp_bridge->mode & AGPSTAT_MODE_3_0)
  556. agp_v3_parse_one(&requested_mode, &bridge_agpstat, &vga_agpstat);
  557. else
  558. agp_v2_parse_one(&requested_mode, &bridge_agpstat, &vga_agpstat);
  559. pci_dev_put(device);
  560. return bridge_agpstat;
  561. }
  562. EXPORT_SYMBOL(agp_collect_device_status);
  563. void agp_device_command(u32 bridge_agpstat, int agp_v3)
  564. {
  565. struct pci_dev *device = NULL;
  566. int mode;
  567. mode = bridge_agpstat & 0x7;
  568. if (agp_v3)
  569. mode *= 4;
  570. for_each_pci_dev(device) {
  571. u8 agp = pci_find_capability(device, PCI_CAP_ID_AGP);
  572. if (!agp)
  573. continue;
  574. printk(KERN_INFO PFX "Putting AGP V%d device at %s into %dx mode\n",
  575. agp_v3 ? 3 : 2, pci_name(device), mode);
  576. pci_write_config_dword(device, agp + PCI_AGP_COMMAND, bridge_agpstat);
  577. }
  578. }
  579. EXPORT_SYMBOL(agp_device_command);
  580. void get_agp_version(struct agp_bridge_data *bridge)
  581. {
  582. u32 ncapid;
  583. /* Exit early if already set by errata workarounds. */
  584. if (bridge->major_version != 0)
  585. return;
  586. pci_read_config_dword(bridge->dev, bridge->capndx, &ncapid);
  587. bridge->major_version = (ncapid >> AGP_MAJOR_VERSION_SHIFT) & 0xf;
  588. bridge->minor_version = (ncapid >> AGP_MINOR_VERSION_SHIFT) & 0xf;
  589. }
  590. EXPORT_SYMBOL(get_agp_version);
  591. void agp_generic_enable(struct agp_bridge_data *bridge, u32 requested_mode)
  592. {
  593. u32 bridge_agpstat, temp;
  594. get_agp_version(agp_bridge);
  595. printk(KERN_INFO PFX "Found an AGP %d.%d compliant device at %s.\n",
  596. agp_bridge->major_version,
  597. agp_bridge->minor_version,
  598. pci_name(agp_bridge->dev));
  599. pci_read_config_dword(agp_bridge->dev,
  600. agp_bridge->capndx + PCI_AGP_STATUS, &bridge_agpstat);
  601. bridge_agpstat = agp_collect_device_status(agp_bridge, requested_mode, bridge_agpstat);
  602. if (bridge_agpstat == 0)
  603. /* Something bad happened. FIXME: Return error code? */
  604. return;
  605. bridge_agpstat |= AGPSTAT_AGP_ENABLE;
  606. /* Do AGP version specific frobbing. */
  607. if (bridge->major_version >= 3) {
  608. if (bridge->mode & AGPSTAT_MODE_3_0) {
  609. /* If we have 3.5, we can do the isoch stuff. */
  610. if (bridge->minor_version >= 5)
  611. agp_3_5_enable(bridge);
  612. agp_device_command(bridge_agpstat, TRUE);
  613. return;
  614. } else {
  615. /* Disable calibration cycle in RX91<1> when not in AGP3.0 mode of operation.*/
  616. bridge_agpstat &= ~(7<<10) ;
  617. pci_read_config_dword(bridge->dev,
  618. bridge->capndx+AGPCTRL, &temp);
  619. temp |= (1<<9);
  620. pci_write_config_dword(bridge->dev,
  621. bridge->capndx+AGPCTRL, temp);
  622. printk(KERN_INFO PFX "Device is in legacy mode,"
  623. " falling back to 2.x\n");
  624. }
  625. }
  626. /* AGP v<3 */
  627. agp_device_command(bridge_agpstat, FALSE);
  628. }
  629. EXPORT_SYMBOL(agp_generic_enable);
  630. int agp_generic_create_gatt_table(struct agp_bridge_data *bridge)
  631. {
  632. char *table;
  633. char *table_end;
  634. int size;
  635. int page_order;
  636. int num_entries;
  637. int i;
  638. void *temp;
  639. struct page *page;
  640. /* The generic routines can't handle 2 level gatt's */
  641. if (bridge->driver->size_type == LVL2_APER_SIZE)
  642. return -EINVAL;
  643. table = NULL;
  644. i = bridge->aperture_size_idx;
  645. temp = bridge->current_size;
  646. size = page_order = num_entries = 0;
  647. if (bridge->driver->size_type != FIXED_APER_SIZE) {
  648. do {
  649. switch (bridge->driver->size_type) {
  650. case U8_APER_SIZE:
  651. size = A_SIZE_8(temp)->size;
  652. page_order =
  653. A_SIZE_8(temp)->page_order;
  654. num_entries =
  655. A_SIZE_8(temp)->num_entries;
  656. break;
  657. case U16_APER_SIZE:
  658. size = A_SIZE_16(temp)->size;
  659. page_order = A_SIZE_16(temp)->page_order;
  660. num_entries = A_SIZE_16(temp)->num_entries;
  661. break;
  662. case U32_APER_SIZE:
  663. size = A_SIZE_32(temp)->size;
  664. page_order = A_SIZE_32(temp)->page_order;
  665. num_entries = A_SIZE_32(temp)->num_entries;
  666. break;
  667. /* This case will never really happen. */
  668. case FIXED_APER_SIZE:
  669. case LVL2_APER_SIZE:
  670. default:
  671. size = page_order = num_entries = 0;
  672. break;
  673. }
  674. table = alloc_gatt_pages(page_order);
  675. if (table == NULL) {
  676. i++;
  677. switch (bridge->driver->size_type) {
  678. case U8_APER_SIZE:
  679. bridge->current_size = A_IDX8(bridge);
  680. break;
  681. case U16_APER_SIZE:
  682. bridge->current_size = A_IDX16(bridge);
  683. break;
  684. case U32_APER_SIZE:
  685. bridge->current_size = A_IDX32(bridge);
  686. break;
  687. /* These cases will never really happen. */
  688. case FIXED_APER_SIZE:
  689. case LVL2_APER_SIZE:
  690. default:
  691. break;
  692. }
  693. temp = bridge->current_size;
  694. } else {
  695. bridge->aperture_size_idx = i;
  696. }
  697. } while (!table && (i < bridge->driver->num_aperture_sizes));
  698. } else {
  699. size = ((struct aper_size_info_fixed *) temp)->size;
  700. page_order = ((struct aper_size_info_fixed *) temp)->page_order;
  701. num_entries = ((struct aper_size_info_fixed *) temp)->num_entries;
  702. table = alloc_gatt_pages(page_order);
  703. }
  704. if (table == NULL)
  705. return -ENOMEM;
  706. table_end = table + ((PAGE_SIZE * (1 << page_order)) - 1);
  707. for (page = virt_to_page(table); page <= virt_to_page(table_end); page++)
  708. SetPageReserved(page);
  709. bridge->gatt_table_real = (u32 *) table;
  710. agp_gatt_table = (void *)table;
  711. bridge->driver->cache_flush();
  712. bridge->gatt_table = ioremap_nocache(virt_to_gart(table),
  713. (PAGE_SIZE * (1 << page_order)));
  714. bridge->driver->cache_flush();
  715. if (bridge->gatt_table == NULL) {
  716. for (page = virt_to_page(table); page <= virt_to_page(table_end); page++)
  717. ClearPageReserved(page);
  718. free_gatt_pages(table, page_order);
  719. return -ENOMEM;
  720. }
  721. bridge->gatt_bus_addr = virt_to_gart(bridge->gatt_table_real);
  722. /* AK: bogus, should encode addresses > 4GB */
  723. for (i = 0; i < num_entries; i++) {
  724. writel(bridge->scratch_page, bridge->gatt_table+i);
  725. readl(bridge->gatt_table+i); /* PCI Posting. */
  726. }
  727. return 0;
  728. }
  729. EXPORT_SYMBOL(agp_generic_create_gatt_table);
  730. int agp_generic_free_gatt_table(struct agp_bridge_data *bridge)
  731. {
  732. int page_order;
  733. char *table, *table_end;
  734. void *temp;
  735. struct page *page;
  736. temp = bridge->current_size;
  737. switch (bridge->driver->size_type) {
  738. case U8_APER_SIZE:
  739. page_order = A_SIZE_8(temp)->page_order;
  740. break;
  741. case U16_APER_SIZE:
  742. page_order = A_SIZE_16(temp)->page_order;
  743. break;
  744. case U32_APER_SIZE:
  745. page_order = A_SIZE_32(temp)->page_order;
  746. break;
  747. case FIXED_APER_SIZE:
  748. page_order = A_SIZE_FIX(temp)->page_order;
  749. break;
  750. case LVL2_APER_SIZE:
  751. /* The generic routines can't deal with 2 level gatt's */
  752. return -EINVAL;
  753. break;
  754. default:
  755. page_order = 0;
  756. break;
  757. }
  758. /* Do not worry about freeing memory, because if this is
  759. * called, then all agp memory is deallocated and removed
  760. * from the table. */
  761. iounmap(bridge->gatt_table);
  762. table = (char *) bridge->gatt_table_real;
  763. table_end = table + ((PAGE_SIZE * (1 << page_order)) - 1);
  764. for (page = virt_to_page(table); page <= virt_to_page(table_end); page++)
  765. ClearPageReserved(page);
  766. free_gatt_pages(bridge->gatt_table_real, page_order);
  767. agp_gatt_table = NULL;
  768. bridge->gatt_table = NULL;
  769. bridge->gatt_table_real = NULL;
  770. bridge->gatt_bus_addr = 0;
  771. return 0;
  772. }
  773. EXPORT_SYMBOL(agp_generic_free_gatt_table);
  774. int agp_generic_insert_memory(struct agp_memory * mem, off_t pg_start, int type)
  775. {
  776. int num_entries;
  777. size_t i;
  778. off_t j;
  779. void *temp;
  780. struct agp_bridge_data *bridge;
  781. bridge = mem->bridge;
  782. if (!bridge)
  783. return -EINVAL;
  784. temp = bridge->current_size;
  785. switch (bridge->driver->size_type) {
  786. case U8_APER_SIZE:
  787. num_entries = A_SIZE_8(temp)->num_entries;
  788. break;
  789. case U16_APER_SIZE:
  790. num_entries = A_SIZE_16(temp)->num_entries;
  791. break;
  792. case U32_APER_SIZE:
  793. num_entries = A_SIZE_32(temp)->num_entries;
  794. break;
  795. case FIXED_APER_SIZE:
  796. num_entries = A_SIZE_FIX(temp)->num_entries;
  797. break;
  798. case LVL2_APER_SIZE:
  799. /* The generic routines can't deal with 2 level gatt's */
  800. return -EINVAL;
  801. break;
  802. default:
  803. num_entries = 0;
  804. break;
  805. }
  806. num_entries -= agp_memory_reserved/PAGE_SIZE;
  807. if (num_entries < 0) num_entries = 0;
  808. if (type != 0 || mem->type != 0) {
  809. /* The generic routines know nothing of memory types */
  810. return -EINVAL;
  811. }
  812. /* AK: could wrap */
  813. if ((pg_start + mem->page_count) > num_entries)
  814. return -EINVAL;
  815. j = pg_start;
  816. while (j < (pg_start + mem->page_count)) {
  817. if (!PGE_EMPTY(bridge, readl(bridge->gatt_table+j)))
  818. return -EBUSY;
  819. j++;
  820. }
  821. if (mem->is_flushed == FALSE) {
  822. bridge->driver->cache_flush();
  823. mem->is_flushed = TRUE;
  824. }
  825. for (i = 0, j = pg_start; i < mem->page_count; i++, j++) {
  826. writel(bridge->driver->mask_memory(bridge, mem->memory[i], mem->type), bridge->gatt_table+j);
  827. readl(bridge->gatt_table+j); /* PCI Posting. */
  828. }
  829. bridge->driver->tlb_flush(mem);
  830. return 0;
  831. }
  832. EXPORT_SYMBOL(agp_generic_insert_memory);
  833. int agp_generic_remove_memory(struct agp_memory *mem, off_t pg_start, int type)
  834. {
  835. size_t i;
  836. struct agp_bridge_data *bridge;
  837. bridge = mem->bridge;
  838. if (!bridge)
  839. return -EINVAL;
  840. if (type != 0 || mem->type != 0) {
  841. /* The generic routines know nothing of memory types */
  842. return -EINVAL;
  843. }
  844. /* AK: bogus, should encode addresses > 4GB */
  845. for (i = pg_start; i < (mem->page_count + pg_start); i++) {
  846. writel(bridge->scratch_page, bridge->gatt_table+i);
  847. readl(bridge->gatt_table+i); /* PCI Posting. */
  848. }
  849. global_cache_flush();
  850. bridge->driver->tlb_flush(mem);
  851. return 0;
  852. }
  853. EXPORT_SYMBOL(agp_generic_remove_memory);
  854. struct agp_memory *agp_generic_alloc_by_type(size_t page_count, int type)
  855. {
  856. return NULL;
  857. }
  858. EXPORT_SYMBOL(agp_generic_alloc_by_type);
  859. void agp_generic_free_by_type(struct agp_memory *curr)
  860. {
  861. vfree(curr->memory);
  862. agp_free_key(curr->key);
  863. kfree(curr);
  864. }
  865. EXPORT_SYMBOL(agp_generic_free_by_type);
  866. /*
  867. * Basic Page Allocation Routines -
  868. * These routines handle page allocation and by default they reserve the allocated
  869. * memory. They also handle incrementing the current_memory_agp value, Which is checked
  870. * against a maximum value.
  871. */
  872. void *agp_generic_alloc_page(struct agp_bridge_data *bridge)
  873. {
  874. struct page * page;
  875. page = alloc_page(GFP_KERNEL);
  876. if (page == NULL)
  877. return NULL;
  878. map_page_into_agp(page);
  879. get_page(page);
  880. SetPageLocked(page);
  881. atomic_inc(&agp_bridge->current_memory_agp);
  882. return page_address(page);
  883. }
  884. EXPORT_SYMBOL(agp_generic_alloc_page);
  885. void agp_generic_destroy_page(void *addr)
  886. {
  887. struct page *page;
  888. if (addr == NULL)
  889. return;
  890. page = virt_to_page(addr);
  891. unmap_page_from_agp(page);
  892. put_page(page);
  893. unlock_page(page);
  894. free_page((unsigned long)addr);
  895. atomic_dec(&agp_bridge->current_memory_agp);
  896. }
  897. EXPORT_SYMBOL(agp_generic_destroy_page);
  898. /* End Basic Page Allocation Routines */
  899. /**
  900. * agp_enable - initialise the agp point-to-point connection.
  901. *
  902. * @mode: agp mode register value to configure with.
  903. */
  904. void agp_enable(struct agp_bridge_data *bridge, u32 mode)
  905. {
  906. if (!bridge)
  907. return;
  908. bridge->driver->agp_enable(bridge, mode);
  909. }
  910. EXPORT_SYMBOL(agp_enable);
  911. /* When we remove the global variable agp_bridge from all drivers
  912. * then agp_alloc_bridge and agp_generic_find_bridge need to be updated
  913. */
  914. struct agp_bridge_data *agp_generic_find_bridge(struct pci_dev *pdev)
  915. {
  916. if (list_empty(&agp_bridges))
  917. return NULL;
  918. return agp_bridge;
  919. }
  920. static void ipi_handler(void *null)
  921. {
  922. flush_agp_cache();
  923. }
  924. void global_cache_flush(void)
  925. {
  926. if (on_each_cpu(ipi_handler, NULL, 1, 1) != 0)
  927. panic(PFX "timed out waiting for the other CPUs!\n");
  928. }
  929. EXPORT_SYMBOL(global_cache_flush);
  930. unsigned long agp_generic_mask_memory(struct agp_bridge_data *bridge,
  931. unsigned long addr, int type)
  932. {
  933. /* memory type is ignored in the generic routine */
  934. if (bridge->driver->masks)
  935. return addr | bridge->driver->masks[0].mask;
  936. else
  937. return addr;
  938. }
  939. EXPORT_SYMBOL(agp_generic_mask_memory);
  940. /*
  941. * These functions are implemented according to the AGPv3 spec,
  942. * which covers implementation details that had previously been
  943. * left open.
  944. */
  945. int agp3_generic_fetch_size(void)
  946. {
  947. u16 temp_size;
  948. int i;
  949. struct aper_size_info_16 *values;
  950. pci_read_config_word(agp_bridge->dev, agp_bridge->capndx+AGPAPSIZE, &temp_size);
  951. values = A_SIZE_16(agp_bridge->driver->aperture_sizes);
  952. for (i = 0; i < agp_bridge->driver->num_aperture_sizes; i++) {
  953. if (temp_size == values[i].size_value) {
  954. agp_bridge->previous_size =
  955. agp_bridge->current_size = (void *) (values + i);
  956. agp_bridge->aperture_size_idx = i;
  957. return values[i].size;
  958. }
  959. }
  960. return 0;
  961. }
  962. EXPORT_SYMBOL(agp3_generic_fetch_size);
  963. void agp3_generic_tlbflush(struct agp_memory *mem)
  964. {
  965. u32 ctrl;
  966. pci_read_config_dword(agp_bridge->dev, agp_bridge->capndx+AGPCTRL, &ctrl);
  967. pci_write_config_dword(agp_bridge->dev, agp_bridge->capndx+AGPCTRL, ctrl & ~AGPCTRL_GTLBEN);
  968. pci_write_config_dword(agp_bridge->dev, agp_bridge->capndx+AGPCTRL, ctrl);
  969. }
  970. EXPORT_SYMBOL(agp3_generic_tlbflush);
  971. int agp3_generic_configure(void)
  972. {
  973. u32 temp;
  974. struct aper_size_info_16 *current_size;
  975. current_size = A_SIZE_16(agp_bridge->current_size);
  976. pci_read_config_dword(agp_bridge->dev, AGP_APBASE, &temp);
  977. agp_bridge->gart_bus_addr = (temp & PCI_BASE_ADDRESS_MEM_MASK);
  978. /* set aperture size */
  979. pci_write_config_word(agp_bridge->dev, agp_bridge->capndx+AGPAPSIZE, current_size->size_value);
  980. /* set gart pointer */
  981. pci_write_config_dword(agp_bridge->dev, agp_bridge->capndx+AGPGARTLO, agp_bridge->gatt_bus_addr);
  982. /* enable aperture and GTLB */
  983. pci_read_config_dword(agp_bridge->dev, agp_bridge->capndx+AGPCTRL, &temp);
  984. pci_write_config_dword(agp_bridge->dev, agp_bridge->capndx+AGPCTRL, temp | AGPCTRL_APERENB | AGPCTRL_GTLBEN);
  985. return 0;
  986. }
  987. EXPORT_SYMBOL(agp3_generic_configure);
  988. void agp3_generic_cleanup(void)
  989. {
  990. u32 ctrl;
  991. pci_read_config_dword(agp_bridge->dev, agp_bridge->capndx+AGPCTRL, &ctrl);
  992. pci_write_config_dword(agp_bridge->dev, agp_bridge->capndx+AGPCTRL, ctrl & ~AGPCTRL_APERENB);
  993. }
  994. EXPORT_SYMBOL(agp3_generic_cleanup);
  995. struct aper_size_info_16 agp3_generic_sizes[AGP_GENERIC_SIZES_ENTRIES] =
  996. {
  997. {4096, 1048576, 10,0x000},
  998. {2048, 524288, 9, 0x800},
  999. {1024, 262144, 8, 0xc00},
  1000. { 512, 131072, 7, 0xe00},
  1001. { 256, 65536, 6, 0xf00},
  1002. { 128, 32768, 5, 0xf20},
  1003. { 64, 16384, 4, 0xf30},
  1004. { 32, 8192, 3, 0xf38},
  1005. { 16, 4096, 2, 0xf3c},
  1006. { 8, 2048, 1, 0xf3e},
  1007. { 4, 1024, 0, 0xf3f}
  1008. };
  1009. EXPORT_SYMBOL(agp3_generic_sizes);