generic.c 36 KB

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