generic.c 36 KB

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