generic.c 36 KB

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