generic.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208
  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/config.h>
  31. #include <linux/module.h>
  32. #include <linux/pci.h>
  33. #include <linux/init.h>
  34. #include <linux/pagemap.h>
  35. #include <linux/miscdevice.h>
  36. #include <linux/pm.h>
  37. #include <linux/agp_backend.h>
  38. #include <linux/vmalloc.h>
  39. #include <linux/dma-mapping.h>
  40. #include <linux/mm.h>
  41. #include <asm/io.h>
  42. #include <asm/cacheflush.h>
  43. #include <asm/pgtable.h>
  44. #include "agp.h"
  45. __u32 *agp_gatt_table;
  46. int agp_memory_reserved;
  47. /*
  48. * Needed by the Nforce GART driver for the time being. Would be
  49. * nice to do this some other way instead of needing this export.
  50. */
  51. EXPORT_SYMBOL_GPL(agp_memory_reserved);
  52. #if defined(CONFIG_X86)
  53. int map_page_into_agp(struct page *page)
  54. {
  55. int i;
  56. i = change_page_attr(page, 1, PAGE_KERNEL_NOCACHE);
  57. /* Caller's responsibility to call global_flush_tlb() for
  58. * performance reasons */
  59. return i;
  60. }
  61. EXPORT_SYMBOL_GPL(map_page_into_agp);
  62. int unmap_page_from_agp(struct page *page)
  63. {
  64. int i;
  65. i = change_page_attr(page, 1, PAGE_KERNEL);
  66. /* Caller's responsibility to call global_flush_tlb() for
  67. * performance reasons */
  68. return i;
  69. }
  70. EXPORT_SYMBOL_GPL(unmap_page_from_agp);
  71. #endif
  72. /*
  73. * Generic routines for handling agp_memory structures -
  74. * They use the basic page allocation routines to do the brunt of the work.
  75. */
  76. void agp_free_key(int key)
  77. {
  78. if (key < 0)
  79. return;
  80. if (key < MAXKEY)
  81. clear_bit(key, agp_bridge->key_list);
  82. }
  83. EXPORT_SYMBOL(agp_free_key);
  84. static int agp_get_key(void)
  85. {
  86. int bit;
  87. bit = find_first_zero_bit(agp_bridge->key_list, MAXKEY);
  88. if (bit < MAXKEY) {
  89. set_bit(bit, agp_bridge->key_list);
  90. return bit;
  91. }
  92. return -1;
  93. }
  94. struct agp_memory *agp_create_memory(int scratch_pages)
  95. {
  96. struct agp_memory *new;
  97. new = kzalloc(sizeof(struct agp_memory), GFP_KERNEL);
  98. if (new == NULL)
  99. return NULL;
  100. new->key = agp_get_key();
  101. if (new->key < 0) {
  102. kfree(new);
  103. return NULL;
  104. }
  105. new->memory = vmalloc(PAGE_SIZE * scratch_pages);
  106. if (new->memory == NULL) {
  107. agp_free_key(new->key);
  108. kfree(new);
  109. return NULL;
  110. }
  111. new->num_scratch_pages = scratch_pages;
  112. return new;
  113. }
  114. EXPORT_SYMBOL(agp_create_memory);
  115. /**
  116. * agp_free_memory - free memory associated with an agp_memory pointer.
  117. *
  118. * @curr: agp_memory pointer to be freed.
  119. *
  120. * It is the only function that can be called when the backend is not owned
  121. * by the caller. (So it can free memory on client death.)
  122. */
  123. void agp_free_memory(struct agp_memory *curr)
  124. {
  125. size_t i;
  126. if (curr == NULL)
  127. return;
  128. if (curr->is_bound == TRUE)
  129. agp_unbind_memory(curr);
  130. if (curr->type != 0) {
  131. curr->bridge->driver->free_by_type(curr);
  132. return;
  133. }
  134. if (curr->page_count != 0) {
  135. for (i = 0; i < curr->page_count; i++) {
  136. curr->bridge->driver->agp_destroy_page(gart_to_virt(curr->memory[i]));
  137. }
  138. flush_agp_mappings();
  139. }
  140. agp_free_key(curr->key);
  141. vfree(curr->memory);
  142. kfree(curr);
  143. }
  144. EXPORT_SYMBOL(agp_free_memory);
  145. #define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(unsigned long))
  146. /**
  147. * agp_allocate_memory - allocate a group of pages of a certain type.
  148. *
  149. * @page_count: size_t argument of the number of pages
  150. * @type: u32 argument of the type of memory to be allocated.
  151. *
  152. * Every agp bridge device will allow you to allocate AGP_NORMAL_MEMORY which
  153. * maps to physical ram. Any other type is device dependent.
  154. *
  155. * It returns NULL whenever memory is unavailable.
  156. */
  157. struct agp_memory *agp_allocate_memory(struct agp_bridge_data *bridge,
  158. size_t page_count, u32 type)
  159. {
  160. int scratch_pages;
  161. struct agp_memory *new;
  162. size_t i;
  163. if (!bridge)
  164. return NULL;
  165. if ((atomic_read(&bridge->current_memory_agp) + page_count) > bridge->max_memory_agp)
  166. return NULL;
  167. if (type != 0) {
  168. new = bridge->driver->alloc_by_type(page_count, type);
  169. if (new)
  170. new->bridge = bridge;
  171. return new;
  172. }
  173. scratch_pages = (page_count + ENTRIES_PER_PAGE - 1) / ENTRIES_PER_PAGE;
  174. new = agp_create_memory(scratch_pages);
  175. if (new == NULL)
  176. return NULL;
  177. for (i = 0; i < page_count; i++) {
  178. void *addr = bridge->driver->agp_alloc_page(bridge);
  179. if (addr == NULL) {
  180. agp_free_memory(new);
  181. return NULL;
  182. }
  183. new->memory[i] = virt_to_gart(addr);
  184. new->page_count++;
  185. }
  186. new->bridge = bridge;
  187. flush_agp_mappings();
  188. return new;
  189. }
  190. EXPORT_SYMBOL(agp_allocate_memory);
  191. /* End - Generic routines for handling agp_memory structures */
  192. static int agp_return_size(void)
  193. {
  194. int current_size;
  195. void *temp;
  196. temp = agp_bridge->current_size;
  197. switch (agp_bridge->driver->size_type) {
  198. case U8_APER_SIZE:
  199. current_size = A_SIZE_8(temp)->size;
  200. break;
  201. case U16_APER_SIZE:
  202. current_size = A_SIZE_16(temp)->size;
  203. break;
  204. case U32_APER_SIZE:
  205. current_size = A_SIZE_32(temp)->size;
  206. break;
  207. case LVL2_APER_SIZE:
  208. current_size = A_SIZE_LVL2(temp)->size;
  209. break;
  210. case FIXED_APER_SIZE:
  211. current_size = A_SIZE_FIX(temp)->size;
  212. break;
  213. default:
  214. current_size = 0;
  215. break;
  216. }
  217. current_size -= (agp_memory_reserved / (1024*1024));
  218. if (current_size <0)
  219. current_size = 0;
  220. return current_size;
  221. }
  222. int agp_num_entries(void)
  223. {
  224. int num_entries;
  225. void *temp;
  226. temp = agp_bridge->current_size;
  227. switch (agp_bridge->driver->size_type) {
  228. case U8_APER_SIZE:
  229. num_entries = A_SIZE_8(temp)->num_entries;
  230. break;
  231. case U16_APER_SIZE:
  232. num_entries = A_SIZE_16(temp)->num_entries;
  233. break;
  234. case U32_APER_SIZE:
  235. num_entries = A_SIZE_32(temp)->num_entries;
  236. break;
  237. case LVL2_APER_SIZE:
  238. num_entries = A_SIZE_LVL2(temp)->num_entries;
  239. break;
  240. case FIXED_APER_SIZE:
  241. num_entries = A_SIZE_FIX(temp)->num_entries;
  242. break;
  243. default:
  244. num_entries = 0;
  245. break;
  246. }
  247. num_entries -= agp_memory_reserved>>PAGE_SHIFT;
  248. if (num_entries<0)
  249. num_entries = 0;
  250. return num_entries;
  251. }
  252. EXPORT_SYMBOL_GPL(agp_num_entries);
  253. /**
  254. * agp_copy_info - copy bridge state information
  255. *
  256. * @info: agp_kern_info pointer. The caller should insure that this pointer is valid.
  257. *
  258. * This function copies information about the agp bridge device and the state of
  259. * the agp backend into an agp_kern_info pointer.
  260. */
  261. int agp_copy_info(struct agp_bridge_data *bridge, struct agp_kern_info *info)
  262. {
  263. memset(info, 0, sizeof(struct agp_kern_info));
  264. if (!bridge) {
  265. info->chipset = NOT_SUPPORTED;
  266. return -EIO;
  267. }
  268. info->version.major = bridge->version->major;
  269. info->version.minor = bridge->version->minor;
  270. info->chipset = SUPPORTED;
  271. info->device = bridge->dev;
  272. if (bridge->mode & AGPSTAT_MODE_3_0)
  273. info->mode = bridge->mode & ~AGP3_RESERVED_MASK;
  274. else
  275. info->mode = bridge->mode & ~AGP2_RESERVED_MASK;
  276. info->aper_base = bridge->gart_bus_addr;
  277. info->aper_size = agp_return_size();
  278. info->max_memory = bridge->max_memory_agp;
  279. info->current_memory = atomic_read(&bridge->current_memory_agp);
  280. info->cant_use_aperture = bridge->driver->cant_use_aperture;
  281. info->vm_ops = bridge->vm_ops;
  282. info->page_mask = ~0UL;
  283. return 0;
  284. }
  285. EXPORT_SYMBOL(agp_copy_info);
  286. /* End - Routine to copy over information structure */
  287. /*
  288. * Routines for handling swapping of agp_memory into the GATT -
  289. * These routines take agp_memory and insert them into the GATT.
  290. * They call device specific routines to actually write to the GATT.
  291. */
  292. /**
  293. * agp_bind_memory - Bind an agp_memory structure into the GATT.
  294. *
  295. * @curr: agp_memory pointer
  296. * @pg_start: an offset into the graphics aperture translation table
  297. *
  298. * It returns -EINVAL if the pointer == NULL.
  299. * It returns -EBUSY if the area of the table requested is already in use.
  300. */
  301. int agp_bind_memory(struct agp_memory *curr, off_t pg_start)
  302. {
  303. int ret_val;
  304. if (curr == NULL)
  305. return -EINVAL;
  306. if (curr->is_bound == TRUE) {
  307. printk(KERN_INFO PFX "memory %p is already bound!\n", curr);
  308. return -EINVAL;
  309. }
  310. if (curr->is_flushed == FALSE) {
  311. curr->bridge->driver->cache_flush();
  312. curr->is_flushed = TRUE;
  313. }
  314. ret_val = curr->bridge->driver->insert_memory(curr, pg_start, curr->type);
  315. if (ret_val != 0)
  316. return ret_val;
  317. curr->is_bound = TRUE;
  318. curr->pg_start = pg_start;
  319. return 0;
  320. }
  321. EXPORT_SYMBOL(agp_bind_memory);
  322. /**
  323. * agp_unbind_memory - Removes an agp_memory structure from the GATT
  324. *
  325. * @curr: agp_memory pointer to be removed from the GATT.
  326. *
  327. * It returns -EINVAL if this piece of agp_memory is not currently bound to
  328. * the graphics aperture translation table or if the agp_memory pointer == NULL
  329. */
  330. int agp_unbind_memory(struct agp_memory *curr)
  331. {
  332. int ret_val;
  333. if (curr == NULL)
  334. return -EINVAL;
  335. if (curr->is_bound != TRUE) {
  336. printk(KERN_INFO PFX "memory %p was not bound!\n", curr);
  337. return -EINVAL;
  338. }
  339. ret_val = curr->bridge->driver->remove_memory(curr, curr->pg_start, curr->type);
  340. if (ret_val != 0)
  341. return ret_val;
  342. curr->is_bound = FALSE;
  343. curr->pg_start = 0;
  344. return 0;
  345. }
  346. EXPORT_SYMBOL(agp_unbind_memory);
  347. /* End - Routines for handling swapping of agp_memory into the GATT */
  348. /* Generic Agp routines - Start */
  349. static void agp_v2_parse_one(u32 *requested_mode, u32 *bridge_agpstat, u32 *vga_agpstat)
  350. {
  351. u32 tmp;
  352. if (*requested_mode & AGP2_RESERVED_MASK) {
  353. printk(KERN_INFO PFX "reserved bits set (%x) in mode 0x%x. Fixed.\n",
  354. *requested_mode & AGP2_RESERVED_MASK, *requested_mode);
  355. *requested_mode &= ~AGP2_RESERVED_MASK;
  356. }
  357. /* Check the speed bits make sense. Only one should be set. */
  358. tmp = *requested_mode & 7;
  359. switch (tmp) {
  360. case 0:
  361. printk(KERN_INFO PFX "%s tried to set rate=x0. Setting to x1 mode.\n", current->comm);
  362. *requested_mode |= AGPSTAT2_1X;
  363. break;
  364. case 1:
  365. case 2:
  366. break;
  367. case 3:
  368. *requested_mode &= ~(AGPSTAT2_1X); /* rate=2 */
  369. break;
  370. case 4:
  371. break;
  372. case 5:
  373. case 6:
  374. case 7:
  375. *requested_mode &= ~(AGPSTAT2_1X|AGPSTAT2_2X); /* rate=4*/
  376. break;
  377. }
  378. /* disable SBA if it's not supported */
  379. if (!((*bridge_agpstat & AGPSTAT_SBA) && (*vga_agpstat & AGPSTAT_SBA) && (*requested_mode & AGPSTAT_SBA)))
  380. *bridge_agpstat &= ~AGPSTAT_SBA;
  381. /* Set rate */
  382. if (!((*bridge_agpstat & AGPSTAT2_4X) && (*vga_agpstat & AGPSTAT2_4X) && (*requested_mode & AGPSTAT2_4X)))
  383. *bridge_agpstat &= ~AGPSTAT2_4X;
  384. if (!((*bridge_agpstat & AGPSTAT2_2X) && (*vga_agpstat & AGPSTAT2_2X) && (*requested_mode & AGPSTAT2_2X)))
  385. *bridge_agpstat &= ~AGPSTAT2_2X;
  386. if (!((*bridge_agpstat & AGPSTAT2_1X) && (*vga_agpstat & AGPSTAT2_1X) && (*requested_mode & AGPSTAT2_1X)))
  387. *bridge_agpstat &= ~AGPSTAT2_1X;
  388. /* Now we know what mode it should be, clear out the unwanted bits. */
  389. if (*bridge_agpstat & AGPSTAT2_4X)
  390. *bridge_agpstat &= ~(AGPSTAT2_1X | AGPSTAT2_2X); /* 4X */
  391. if (*bridge_agpstat & AGPSTAT2_2X)
  392. *bridge_agpstat &= ~(AGPSTAT2_1X | AGPSTAT2_4X); /* 2X */
  393. if (*bridge_agpstat & AGPSTAT2_1X)
  394. *bridge_agpstat &= ~(AGPSTAT2_2X | AGPSTAT2_4X); /* 1X */
  395. /* Apply any errata. */
  396. if (agp_bridge->flags & AGP_ERRATA_FASTWRITES)
  397. *bridge_agpstat &= ~AGPSTAT_FW;
  398. if (agp_bridge->flags & AGP_ERRATA_SBA)
  399. *bridge_agpstat &= ~AGPSTAT_SBA;
  400. if (agp_bridge->flags & AGP_ERRATA_1X) {
  401. *bridge_agpstat &= ~(AGPSTAT2_2X | AGPSTAT2_4X);
  402. *bridge_agpstat |= AGPSTAT2_1X;
  403. }
  404. /* If we've dropped down to 1X, disable fast writes. */
  405. if (*bridge_agpstat & AGPSTAT2_1X)
  406. *bridge_agpstat &= ~AGPSTAT_FW;
  407. }
  408. /*
  409. * requested_mode = Mode requested by (typically) X.
  410. * bridge_agpstat = PCI_AGP_STATUS from agp bridge.
  411. * vga_agpstat = PCI_AGP_STATUS from graphic card.
  412. */
  413. static void agp_v3_parse_one(u32 *requested_mode, u32 *bridge_agpstat, u32 *vga_agpstat)
  414. {
  415. u32 origbridge=*bridge_agpstat, origvga=*vga_agpstat;
  416. u32 tmp;
  417. if (*requested_mode & AGP3_RESERVED_MASK) {
  418. printk(KERN_INFO PFX "reserved bits set (%x) in mode 0x%x. Fixed.\n",
  419. *requested_mode & AGP3_RESERVED_MASK, *requested_mode);
  420. *requested_mode &= ~AGP3_RESERVED_MASK;
  421. }
  422. /* Check the speed bits make sense. */
  423. tmp = *requested_mode & 7;
  424. if (tmp == 0) {
  425. printk(KERN_INFO PFX "%s tried to set rate=x0. Setting to AGP3 x4 mode.\n", current->comm);
  426. *requested_mode |= AGPSTAT3_4X;
  427. }
  428. if (tmp >= 3) {
  429. printk(KERN_INFO PFX "%s tried to set rate=x%d. Setting to AGP3 x8 mode.\n", current->comm, tmp * 4);
  430. *requested_mode = (*requested_mode & ~7) | AGPSTAT3_8X;
  431. }
  432. /* ARQSZ - Set the value to the maximum one.
  433. * Don't allow the mode register to override values. */
  434. *bridge_agpstat = ((*bridge_agpstat & ~AGPSTAT_ARQSZ) |
  435. max_t(u32,(*bridge_agpstat & AGPSTAT_ARQSZ),(*vga_agpstat & AGPSTAT_ARQSZ)));
  436. /* Calibration cycle.
  437. * Don't allow the mode register to override values. */
  438. *bridge_agpstat = ((*bridge_agpstat & ~AGPSTAT_CAL_MASK) |
  439. min_t(u32,(*bridge_agpstat & AGPSTAT_CAL_MASK),(*vga_agpstat & AGPSTAT_CAL_MASK)));
  440. /* SBA *must* be supported for AGP v3 */
  441. *bridge_agpstat |= AGPSTAT_SBA;
  442. /*
  443. * Set speed.
  444. * Check for invalid speeds. This can happen when applications
  445. * written before the AGP 3.0 standard pass AGP2.x modes to AGP3 hardware
  446. */
  447. if (*requested_mode & AGPSTAT_MODE_3_0) {
  448. /*
  449. * Caller hasn't a clue what it is doing. Bridge is in 3.0 mode,
  450. * have been passed a 3.0 mode, but with 2.x speed bits set.
  451. * AGP2.x 4x -> AGP3.0 4x.
  452. */
  453. if (*requested_mode & AGPSTAT2_4X) {
  454. printk(KERN_INFO PFX "%s passes broken AGP3 flags (%x). Fixed.\n",
  455. current->comm, *requested_mode);
  456. *requested_mode &= ~AGPSTAT2_4X;
  457. *requested_mode |= AGPSTAT3_4X;
  458. }
  459. } else {
  460. /*
  461. * The caller doesn't know what they are doing. We are in 3.0 mode,
  462. * but have been passed an AGP 2.x mode.
  463. * Convert AGP 1x,2x,4x -> AGP 3.0 4x.
  464. */
  465. printk(KERN_INFO PFX "%s passes broken AGP2 flags (%x) in AGP3 mode. Fixed.\n",
  466. current->comm, *requested_mode);
  467. *requested_mode &= ~(AGPSTAT2_4X | AGPSTAT2_2X | AGPSTAT2_1X);
  468. *requested_mode |= AGPSTAT3_4X;
  469. }
  470. if (*requested_mode & AGPSTAT3_8X) {
  471. if (!(*bridge_agpstat & AGPSTAT3_8X)) {
  472. *bridge_agpstat &= ~(AGPSTAT3_8X | AGPSTAT3_RSVD);
  473. *bridge_agpstat |= AGPSTAT3_4X;
  474. printk(KERN_INFO PFX "%s requested AGPx8 but bridge not capable.\n", current->comm);
  475. return;
  476. }
  477. if (!(*vga_agpstat & AGPSTAT3_8X)) {
  478. *bridge_agpstat &= ~(AGPSTAT3_8X | AGPSTAT3_RSVD);
  479. *bridge_agpstat |= AGPSTAT3_4X;
  480. printk(KERN_INFO PFX "%s requested AGPx8 but graphic card not capable.\n", current->comm);
  481. return;
  482. }
  483. /* All set, bridge & device can do AGP x8*/
  484. *bridge_agpstat &= ~(AGPSTAT3_4X | AGPSTAT3_RSVD);
  485. goto done;
  486. } else {
  487. /*
  488. * If we didn't specify AGPx8, we can only do x4.
  489. * If the hardware can't do x4, we're up shit creek, and never
  490. * should have got this far.
  491. */
  492. *bridge_agpstat &= ~(AGPSTAT3_8X | AGPSTAT3_RSVD);
  493. if ((*bridge_agpstat & AGPSTAT3_4X) && (*vga_agpstat & AGPSTAT3_4X))
  494. *bridge_agpstat |= AGPSTAT3_4X;
  495. else {
  496. printk(KERN_INFO PFX "Badness. Don't know which AGP mode to set. "
  497. "[bridge_agpstat:%x vga_agpstat:%x fell back to:- bridge_agpstat:%x vga_agpstat:%x]\n",
  498. origbridge, origvga, *bridge_agpstat, *vga_agpstat);
  499. if (!(*bridge_agpstat & AGPSTAT3_4X))
  500. printk(KERN_INFO PFX "Bridge couldn't do AGP x4.\n");
  501. if (!(*vga_agpstat & AGPSTAT3_4X))
  502. printk(KERN_INFO PFX "Graphic card couldn't do AGP x4.\n");
  503. return;
  504. }
  505. }
  506. done:
  507. /* Apply any errata. */
  508. if (agp_bridge->flags & AGP_ERRATA_FASTWRITES)
  509. *bridge_agpstat &= ~AGPSTAT_FW;
  510. if (agp_bridge->flags & AGP_ERRATA_SBA)
  511. *bridge_agpstat &= ~AGPSTAT_SBA;
  512. if (agp_bridge->flags & AGP_ERRATA_1X) {
  513. *bridge_agpstat &= ~(AGPSTAT2_2X | AGPSTAT2_4X);
  514. *bridge_agpstat |= AGPSTAT2_1X;
  515. }
  516. }
  517. /**
  518. * agp_collect_device_status - determine correct agp_cmd from various agp_stat's
  519. * @bridge: an agp_bridge_data struct allocated for the AGP host bridge.
  520. * @requested_mode: requested agp_stat from userspace (Typically from X)
  521. * @bridge_agpstat: current agp_stat from AGP bridge.
  522. *
  523. * This function will hunt for an AGP graphics card, and try to match
  524. * the requested mode to the capabilities of both the bridge and the card.
  525. */
  526. u32 agp_collect_device_status(struct agp_bridge_data *bridge, u32 requested_mode, u32 bridge_agpstat)
  527. {
  528. struct pci_dev *device = NULL;
  529. u32 vga_agpstat;
  530. u8 cap_ptr;
  531. for (;;) {
  532. device = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, device);
  533. if (!device) {
  534. printk(KERN_INFO PFX "Couldn't find an AGP VGA controller.\n");
  535. return 0;
  536. }
  537. cap_ptr = pci_find_capability(device, PCI_CAP_ID_AGP);
  538. if (cap_ptr)
  539. break;
  540. }
  541. /*
  542. * Ok, here we have a AGP device. Disable impossible
  543. * settings, and adjust the readqueue to the minimum.
  544. */
  545. pci_read_config_dword(device, cap_ptr+PCI_AGP_STATUS, &vga_agpstat);
  546. /* adjust RQ depth */
  547. bridge_agpstat = ((bridge_agpstat & ~AGPSTAT_RQ_DEPTH) |
  548. min_t(u32, (requested_mode & AGPSTAT_RQ_DEPTH),
  549. min_t(u32, (bridge_agpstat & AGPSTAT_RQ_DEPTH), (vga_agpstat & AGPSTAT_RQ_DEPTH))));
  550. /* disable FW if it's not supported */
  551. if (!((bridge_agpstat & AGPSTAT_FW) &&
  552. (vga_agpstat & AGPSTAT_FW) &&
  553. (requested_mode & AGPSTAT_FW)))
  554. bridge_agpstat &= ~AGPSTAT_FW;
  555. /* Check to see if we are operating in 3.0 mode */
  556. if (agp_bridge->mode & AGPSTAT_MODE_3_0)
  557. agp_v3_parse_one(&requested_mode, &bridge_agpstat, &vga_agpstat);
  558. else
  559. agp_v2_parse_one(&requested_mode, &bridge_agpstat, &vga_agpstat);
  560. pci_dev_put(device);
  561. return bridge_agpstat;
  562. }
  563. EXPORT_SYMBOL(agp_collect_device_status);
  564. void agp_device_command(u32 bridge_agpstat, int agp_v3)
  565. {
  566. struct pci_dev *device = NULL;
  567. int mode;
  568. mode = bridge_agpstat & 0x7;
  569. if (agp_v3)
  570. mode *= 4;
  571. for_each_pci_dev(device) {
  572. u8 agp = pci_find_capability(device, PCI_CAP_ID_AGP);
  573. if (!agp)
  574. continue;
  575. printk(KERN_INFO PFX "Putting AGP V%d device at %s into %dx mode\n",
  576. agp_v3 ? 3 : 2, pci_name(device), mode);
  577. pci_write_config_dword(device, agp + PCI_AGP_COMMAND, bridge_agpstat);
  578. }
  579. }
  580. EXPORT_SYMBOL(agp_device_command);
  581. void get_agp_version(struct agp_bridge_data *bridge)
  582. {
  583. u32 ncapid;
  584. /* Exit early if already set by errata workarounds. */
  585. if (bridge->major_version != 0)
  586. return;
  587. pci_read_config_dword(bridge->dev, bridge->capndx, &ncapid);
  588. bridge->major_version = (ncapid >> AGP_MAJOR_VERSION_SHIFT) & 0xf;
  589. bridge->minor_version = (ncapid >> AGP_MINOR_VERSION_SHIFT) & 0xf;
  590. }
  591. EXPORT_SYMBOL(get_agp_version);
  592. void agp_generic_enable(struct agp_bridge_data *bridge, u32 requested_mode)
  593. {
  594. u32 bridge_agpstat, temp;
  595. get_agp_version(agp_bridge);
  596. printk(KERN_INFO PFX "Found an AGP %d.%d compliant device at %s.\n",
  597. agp_bridge->major_version,
  598. agp_bridge->minor_version,
  599. pci_name(agp_bridge->dev));
  600. pci_read_config_dword(agp_bridge->dev,
  601. agp_bridge->capndx + PCI_AGP_STATUS, &bridge_agpstat);
  602. bridge_agpstat = agp_collect_device_status(agp_bridge, requested_mode, bridge_agpstat);
  603. if (bridge_agpstat == 0)
  604. /* Something bad happened. FIXME: Return error code? */
  605. return;
  606. bridge_agpstat |= AGPSTAT_AGP_ENABLE;
  607. /* Do AGP version specific frobbing. */
  608. if (bridge->major_version >= 3) {
  609. if (bridge->mode & AGPSTAT_MODE_3_0) {
  610. /* If we have 3.5, we can do the isoch stuff. */
  611. if (bridge->minor_version >= 5)
  612. agp_3_5_enable(bridge);
  613. agp_device_command(bridge_agpstat, TRUE);
  614. return;
  615. } else {
  616. /* Disable calibration cycle in RX91<1> when not in AGP3.0 mode of operation.*/
  617. bridge_agpstat &= ~(7<<10) ;
  618. pci_read_config_dword(bridge->dev,
  619. bridge->capndx+AGPCTRL, &temp);
  620. temp |= (1<<9);
  621. pci_write_config_dword(bridge->dev,
  622. bridge->capndx+AGPCTRL, temp);
  623. printk(KERN_INFO PFX "Device is in legacy mode,"
  624. " falling back to 2.x\n");
  625. }
  626. }
  627. /* AGP v<3 */
  628. agp_device_command(bridge_agpstat, FALSE);
  629. }
  630. EXPORT_SYMBOL(agp_generic_enable);
  631. int agp_generic_create_gatt_table(struct agp_bridge_data *bridge)
  632. {
  633. char *table;
  634. char *table_end;
  635. int size;
  636. int page_order;
  637. int num_entries;
  638. int i;
  639. void *temp;
  640. struct page *page;
  641. /* The generic routines can't handle 2 level gatt's */
  642. if (bridge->driver->size_type == LVL2_APER_SIZE)
  643. return -EINVAL;
  644. table = NULL;
  645. i = bridge->aperture_size_idx;
  646. temp = bridge->current_size;
  647. size = page_order = num_entries = 0;
  648. if (bridge->driver->size_type != FIXED_APER_SIZE) {
  649. do {
  650. switch (bridge->driver->size_type) {
  651. case U8_APER_SIZE:
  652. size = A_SIZE_8(temp)->size;
  653. page_order =
  654. A_SIZE_8(temp)->page_order;
  655. num_entries =
  656. A_SIZE_8(temp)->num_entries;
  657. break;
  658. case U16_APER_SIZE:
  659. size = A_SIZE_16(temp)->size;
  660. page_order = A_SIZE_16(temp)->page_order;
  661. num_entries = A_SIZE_16(temp)->num_entries;
  662. break;
  663. case U32_APER_SIZE:
  664. size = A_SIZE_32(temp)->size;
  665. page_order = A_SIZE_32(temp)->page_order;
  666. num_entries = A_SIZE_32(temp)->num_entries;
  667. break;
  668. /* This case will never really happen. */
  669. case FIXED_APER_SIZE:
  670. case LVL2_APER_SIZE:
  671. default:
  672. size = page_order = num_entries = 0;
  673. break;
  674. }
  675. table = alloc_gatt_pages(page_order);
  676. if (table == NULL) {
  677. i++;
  678. switch (bridge->driver->size_type) {
  679. case U8_APER_SIZE:
  680. bridge->current_size = A_IDX8(bridge);
  681. break;
  682. case U16_APER_SIZE:
  683. bridge->current_size = A_IDX16(bridge);
  684. break;
  685. case U32_APER_SIZE:
  686. bridge->current_size = A_IDX32(bridge);
  687. break;
  688. /* These cases will never really happen. */
  689. case FIXED_APER_SIZE:
  690. case LVL2_APER_SIZE:
  691. default:
  692. break;
  693. }
  694. temp = bridge->current_size;
  695. } else {
  696. bridge->aperture_size_idx = i;
  697. }
  698. } while (!table && (i < bridge->driver->num_aperture_sizes));
  699. } else {
  700. size = ((struct aper_size_info_fixed *) temp)->size;
  701. page_order = ((struct aper_size_info_fixed *) temp)->page_order;
  702. num_entries = ((struct aper_size_info_fixed *) temp)->num_entries;
  703. table = alloc_gatt_pages(page_order);
  704. }
  705. if (table == NULL)
  706. return -ENOMEM;
  707. table_end = table + ((PAGE_SIZE * (1 << page_order)) - 1);
  708. for (page = virt_to_page(table); page <= virt_to_page(table_end); page++)
  709. SetPageReserved(page);
  710. bridge->gatt_table_real = (u32 *) table;
  711. agp_gatt_table = (void *)table;
  712. bridge->driver->cache_flush();
  713. bridge->gatt_table = ioremap_nocache(virt_to_gart(table),
  714. (PAGE_SIZE * (1 << page_order)));
  715. bridge->driver->cache_flush();
  716. if (bridge->gatt_table == NULL) {
  717. for (page = virt_to_page(table); page <= virt_to_page(table_end); page++)
  718. ClearPageReserved(page);
  719. free_gatt_pages(table, page_order);
  720. return -ENOMEM;
  721. }
  722. bridge->gatt_bus_addr = virt_to_gart(bridge->gatt_table_real);
  723. /* AK: bogus, should encode addresses > 4GB */
  724. for (i = 0; i < num_entries; i++) {
  725. writel(bridge->scratch_page, bridge->gatt_table+i);
  726. readl(bridge->gatt_table+i); /* PCI Posting. */
  727. }
  728. return 0;
  729. }
  730. EXPORT_SYMBOL(agp_generic_create_gatt_table);
  731. int agp_generic_free_gatt_table(struct agp_bridge_data *bridge)
  732. {
  733. int page_order;
  734. char *table, *table_end;
  735. void *temp;
  736. struct page *page;
  737. temp = bridge->current_size;
  738. switch (bridge->driver->size_type) {
  739. case U8_APER_SIZE:
  740. page_order = A_SIZE_8(temp)->page_order;
  741. break;
  742. case U16_APER_SIZE:
  743. page_order = A_SIZE_16(temp)->page_order;
  744. break;
  745. case U32_APER_SIZE:
  746. page_order = A_SIZE_32(temp)->page_order;
  747. break;
  748. case FIXED_APER_SIZE:
  749. page_order = A_SIZE_FIX(temp)->page_order;
  750. break;
  751. case LVL2_APER_SIZE:
  752. /* The generic routines can't deal with 2 level gatt's */
  753. return -EINVAL;
  754. break;
  755. default:
  756. page_order = 0;
  757. break;
  758. }
  759. /* Do not worry about freeing memory, because if this is
  760. * called, then all agp memory is deallocated and removed
  761. * from the table. */
  762. iounmap(bridge->gatt_table);
  763. table = (char *) bridge->gatt_table_real;
  764. table_end = table + ((PAGE_SIZE * (1 << page_order)) - 1);
  765. for (page = virt_to_page(table); page <= virt_to_page(table_end); page++)
  766. ClearPageReserved(page);
  767. free_gatt_pages(bridge->gatt_table_real, page_order);
  768. agp_gatt_table = NULL;
  769. bridge->gatt_table = NULL;
  770. bridge->gatt_table_real = NULL;
  771. bridge->gatt_bus_addr = 0;
  772. return 0;
  773. }
  774. EXPORT_SYMBOL(agp_generic_free_gatt_table);
  775. int agp_generic_insert_memory(struct agp_memory * mem, off_t pg_start, int type)
  776. {
  777. int num_entries;
  778. size_t i;
  779. off_t j;
  780. void *temp;
  781. struct agp_bridge_data *bridge;
  782. bridge = mem->bridge;
  783. if (!bridge)
  784. return -EINVAL;
  785. temp = bridge->current_size;
  786. switch (bridge->driver->size_type) {
  787. case U8_APER_SIZE:
  788. num_entries = A_SIZE_8(temp)->num_entries;
  789. break;
  790. case U16_APER_SIZE:
  791. num_entries = A_SIZE_16(temp)->num_entries;
  792. break;
  793. case U32_APER_SIZE:
  794. num_entries = A_SIZE_32(temp)->num_entries;
  795. break;
  796. case FIXED_APER_SIZE:
  797. num_entries = A_SIZE_FIX(temp)->num_entries;
  798. break;
  799. case LVL2_APER_SIZE:
  800. /* The generic routines can't deal with 2 level gatt's */
  801. return -EINVAL;
  802. break;
  803. default:
  804. num_entries = 0;
  805. break;
  806. }
  807. num_entries -= agp_memory_reserved/PAGE_SIZE;
  808. if (num_entries < 0) num_entries = 0;
  809. if (type != 0 || mem->type != 0) {
  810. /* The generic routines know nothing of memory types */
  811. return -EINVAL;
  812. }
  813. /* AK: could wrap */
  814. if ((pg_start + mem->page_count) > num_entries)
  815. return -EINVAL;
  816. j = pg_start;
  817. while (j < (pg_start + mem->page_count)) {
  818. if (!PGE_EMPTY(bridge, readl(bridge->gatt_table+j)))
  819. return -EBUSY;
  820. j++;
  821. }
  822. if (mem->is_flushed == FALSE) {
  823. bridge->driver->cache_flush();
  824. mem->is_flushed = TRUE;
  825. }
  826. for (i = 0, j = pg_start; i < mem->page_count; i++, j++) {
  827. writel(bridge->driver->mask_memory(bridge, mem->memory[i], mem->type), bridge->gatt_table+j);
  828. readl(bridge->gatt_table+j); /* PCI Posting. */
  829. }
  830. bridge->driver->tlb_flush(mem);
  831. return 0;
  832. }
  833. EXPORT_SYMBOL(agp_generic_insert_memory);
  834. int agp_generic_remove_memory(struct agp_memory *mem, off_t pg_start, int type)
  835. {
  836. size_t i;
  837. struct agp_bridge_data *bridge;
  838. bridge = mem->bridge;
  839. if (!bridge)
  840. return -EINVAL;
  841. if (type != 0 || mem->type != 0) {
  842. /* The generic routines know nothing of memory types */
  843. return -EINVAL;
  844. }
  845. /* AK: bogus, should encode addresses > 4GB */
  846. for (i = pg_start; i < (mem->page_count + pg_start); i++) {
  847. writel(bridge->scratch_page, bridge->gatt_table+i);
  848. readl(bridge->gatt_table+i); /* PCI Posting. */
  849. }
  850. global_cache_flush();
  851. bridge->driver->tlb_flush(mem);
  852. return 0;
  853. }
  854. EXPORT_SYMBOL(agp_generic_remove_memory);
  855. struct agp_memory *agp_generic_alloc_by_type(size_t page_count, int type)
  856. {
  857. return NULL;
  858. }
  859. EXPORT_SYMBOL(agp_generic_alloc_by_type);
  860. void agp_generic_free_by_type(struct agp_memory *curr)
  861. {
  862. vfree(curr->memory);
  863. agp_free_key(curr->key);
  864. kfree(curr);
  865. }
  866. EXPORT_SYMBOL(agp_generic_free_by_type);
  867. /*
  868. * Basic Page Allocation Routines -
  869. * These routines handle page allocation and by default they reserve the allocated
  870. * memory. They also handle incrementing the current_memory_agp value, Which is checked
  871. * against a maximum value.
  872. */
  873. void *agp_generic_alloc_page(struct agp_bridge_data *bridge)
  874. {
  875. struct page * page;
  876. page = alloc_page(GFP_KERNEL);
  877. if (page == NULL)
  878. return NULL;
  879. map_page_into_agp(page);
  880. get_page(page);
  881. SetPageLocked(page);
  882. atomic_inc(&agp_bridge->current_memory_agp);
  883. return page_address(page);
  884. }
  885. EXPORT_SYMBOL(agp_generic_alloc_page);
  886. void agp_generic_destroy_page(void *addr)
  887. {
  888. struct page *page;
  889. if (addr == NULL)
  890. return;
  891. page = virt_to_page(addr);
  892. unmap_page_from_agp(page);
  893. put_page(page);
  894. unlock_page(page);
  895. free_page((unsigned long)addr);
  896. atomic_dec(&agp_bridge->current_memory_agp);
  897. }
  898. EXPORT_SYMBOL(agp_generic_destroy_page);
  899. /* End Basic Page Allocation Routines */
  900. /**
  901. * agp_enable - initialise the agp point-to-point connection.
  902. *
  903. * @mode: agp mode register value to configure with.
  904. */
  905. void agp_enable(struct agp_bridge_data *bridge, u32 mode)
  906. {
  907. if (!bridge)
  908. return;
  909. bridge->driver->agp_enable(bridge, mode);
  910. }
  911. EXPORT_SYMBOL(agp_enable);
  912. /* When we remove the global variable agp_bridge from all drivers
  913. * then agp_alloc_bridge and agp_generic_find_bridge need to be updated
  914. */
  915. struct agp_bridge_data *agp_generic_find_bridge(struct pci_dev *pdev)
  916. {
  917. if (list_empty(&agp_bridges))
  918. return NULL;
  919. return agp_bridge;
  920. }
  921. static void ipi_handler(void *null)
  922. {
  923. flush_agp_cache();
  924. }
  925. void global_cache_flush(void)
  926. {
  927. if (on_each_cpu(ipi_handler, NULL, 1, 1) != 0)
  928. panic(PFX "timed out waiting for the other CPUs!\n");
  929. }
  930. EXPORT_SYMBOL(global_cache_flush);
  931. unsigned long agp_generic_mask_memory(struct agp_bridge_data *bridge,
  932. unsigned long addr, int type)
  933. {
  934. /* memory type is ignored in the generic routine */
  935. if (bridge->driver->masks)
  936. return addr | bridge->driver->masks[0].mask;
  937. else
  938. return addr;
  939. }
  940. EXPORT_SYMBOL(agp_generic_mask_memory);
  941. /*
  942. * These functions are implemented according to the AGPv3 spec,
  943. * which covers implementation details that had previously been
  944. * left open.
  945. */
  946. int agp3_generic_fetch_size(void)
  947. {
  948. u16 temp_size;
  949. int i;
  950. struct aper_size_info_16 *values;
  951. pci_read_config_word(agp_bridge->dev, agp_bridge->capndx+AGPAPSIZE, &temp_size);
  952. values = A_SIZE_16(agp_bridge->driver->aperture_sizes);
  953. for (i = 0; i < agp_bridge->driver->num_aperture_sizes; i++) {
  954. if (temp_size == values[i].size_value) {
  955. agp_bridge->previous_size =
  956. agp_bridge->current_size = (void *) (values + i);
  957. agp_bridge->aperture_size_idx = i;
  958. return values[i].size;
  959. }
  960. }
  961. return 0;
  962. }
  963. EXPORT_SYMBOL(agp3_generic_fetch_size);
  964. void agp3_generic_tlbflush(struct agp_memory *mem)
  965. {
  966. u32 ctrl;
  967. pci_read_config_dword(agp_bridge->dev, agp_bridge->capndx+AGPCTRL, &ctrl);
  968. pci_write_config_dword(agp_bridge->dev, agp_bridge->capndx+AGPCTRL, ctrl & ~AGPCTRL_GTLBEN);
  969. pci_write_config_dword(agp_bridge->dev, agp_bridge->capndx+AGPCTRL, ctrl);
  970. }
  971. EXPORT_SYMBOL(agp3_generic_tlbflush);
  972. int agp3_generic_configure(void)
  973. {
  974. u32 temp;
  975. struct aper_size_info_16 *current_size;
  976. current_size = A_SIZE_16(agp_bridge->current_size);
  977. pci_read_config_dword(agp_bridge->dev, AGP_APBASE, &temp);
  978. agp_bridge->gart_bus_addr = (temp & PCI_BASE_ADDRESS_MEM_MASK);
  979. /* set aperture size */
  980. pci_write_config_word(agp_bridge->dev, agp_bridge->capndx+AGPAPSIZE, current_size->size_value);
  981. /* set gart pointer */
  982. pci_write_config_dword(agp_bridge->dev, agp_bridge->capndx+AGPGARTLO, agp_bridge->gatt_bus_addr);
  983. /* enable aperture and GTLB */
  984. pci_read_config_dword(agp_bridge->dev, agp_bridge->capndx+AGPCTRL, &temp);
  985. pci_write_config_dword(agp_bridge->dev, agp_bridge->capndx+AGPCTRL, temp | AGPCTRL_APERENB | AGPCTRL_GTLBEN);
  986. return 0;
  987. }
  988. EXPORT_SYMBOL(agp3_generic_configure);
  989. void agp3_generic_cleanup(void)
  990. {
  991. u32 ctrl;
  992. pci_read_config_dword(agp_bridge->dev, agp_bridge->capndx+AGPCTRL, &ctrl);
  993. pci_write_config_dword(agp_bridge->dev, agp_bridge->capndx+AGPCTRL, ctrl & ~AGPCTRL_APERENB);
  994. }
  995. EXPORT_SYMBOL(agp3_generic_cleanup);
  996. struct aper_size_info_16 agp3_generic_sizes[AGP_GENERIC_SIZES_ENTRIES] =
  997. {
  998. {4096, 1048576, 10,0x000},
  999. {2048, 524288, 9, 0x800},
  1000. {1024, 262144, 8, 0xc00},
  1001. { 512, 131072, 7, 0xe00},
  1002. { 256, 65536, 6, 0xf00},
  1003. { 128, 32768, 5, 0xf20},
  1004. { 64, 16384, 4, 0xf30},
  1005. { 32, 8192, 3, 0xf38},
  1006. { 16, 4096, 2, 0xf3c},
  1007. { 8, 2048, 1, 0xf3e},
  1008. { 4, 1024, 0, 0xf3f}
  1009. };
  1010. EXPORT_SYMBOL(agp3_generic_sizes);