generic.c 37 KB

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