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