generic.c 37 KB

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