generic.c 37 KB

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