generic.c 37 KB

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