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