generic.c 36 KB

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