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