generic.c 37 KB

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