sworks-agp.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. /*
  2. * Serverworks AGPGART routines.
  3. */
  4. #include <linux/module.h>
  5. #include <linux/pci.h>
  6. #include <linux/init.h>
  7. #include <linux/string.h>
  8. #include <linux/slab.h>
  9. #include <linux/jiffies.h>
  10. #include <linux/agp_backend.h>
  11. #include "agp.h"
  12. #define SVWRKS_COMMAND 0x04
  13. #define SVWRKS_APSIZE 0x10
  14. #define SVWRKS_MMBASE 0x14
  15. #define SVWRKS_CACHING 0x4b
  16. #define SVWRKS_AGP_ENABLE 0x60
  17. #define SVWRKS_FEATURE 0x68
  18. #define SVWRKS_SIZE_MASK 0xfe000000
  19. /* Memory mapped registers */
  20. #define SVWRKS_GART_CACHE 0x02
  21. #define SVWRKS_GATTBASE 0x04
  22. #define SVWRKS_TLBFLUSH 0x10
  23. #define SVWRKS_POSTFLUSH 0x14
  24. #define SVWRKS_DIRFLUSH 0x0c
  25. struct serverworks_page_map {
  26. unsigned long *real;
  27. unsigned long __iomem *remapped;
  28. };
  29. static struct _serverworks_private {
  30. struct pci_dev *svrwrks_dev; /* device one */
  31. volatile u8 __iomem *registers;
  32. struct serverworks_page_map **gatt_pages;
  33. int num_tables;
  34. struct serverworks_page_map scratch_dir;
  35. int gart_addr_ofs;
  36. int mm_addr_ofs;
  37. } serverworks_private;
  38. static int serverworks_create_page_map(struct serverworks_page_map *page_map)
  39. {
  40. int i;
  41. page_map->real = (unsigned long *) __get_free_page(GFP_KERNEL);
  42. if (page_map->real == NULL) {
  43. return -ENOMEM;
  44. }
  45. set_memory_uc((unsigned long)page_map->real, 1);
  46. page_map->remapped = page_map->real;
  47. for (i = 0; i < PAGE_SIZE / sizeof(unsigned long); i++)
  48. writel(agp_bridge->scratch_page, page_map->remapped+i);
  49. /* Red Pen: Everyone else does pci posting flush here */
  50. return 0;
  51. }
  52. static void serverworks_free_page_map(struct serverworks_page_map *page_map)
  53. {
  54. set_memory_wb((unsigned long)page_map->real, 1);
  55. free_page((unsigned long) page_map->real);
  56. }
  57. static void serverworks_free_gatt_pages(void)
  58. {
  59. int i;
  60. struct serverworks_page_map **tables;
  61. struct serverworks_page_map *entry;
  62. tables = serverworks_private.gatt_pages;
  63. for (i = 0; i < serverworks_private.num_tables; i++) {
  64. entry = tables[i];
  65. if (entry != NULL) {
  66. if (entry->real != NULL) {
  67. serverworks_free_page_map(entry);
  68. }
  69. kfree(entry);
  70. }
  71. }
  72. kfree(tables);
  73. }
  74. static int serverworks_create_gatt_pages(int nr_tables)
  75. {
  76. struct serverworks_page_map **tables;
  77. struct serverworks_page_map *entry;
  78. int retval = 0;
  79. int i;
  80. tables = kzalloc((nr_tables + 1) * sizeof(struct serverworks_page_map *),
  81. GFP_KERNEL);
  82. if (tables == NULL)
  83. return -ENOMEM;
  84. for (i = 0; i < nr_tables; i++) {
  85. entry = kzalloc(sizeof(struct serverworks_page_map), GFP_KERNEL);
  86. if (entry == NULL) {
  87. retval = -ENOMEM;
  88. break;
  89. }
  90. tables[i] = entry;
  91. retval = serverworks_create_page_map(entry);
  92. if (retval != 0) break;
  93. }
  94. serverworks_private.num_tables = nr_tables;
  95. serverworks_private.gatt_pages = tables;
  96. if (retval != 0) serverworks_free_gatt_pages();
  97. return retval;
  98. }
  99. #define SVRWRKS_GET_GATT(addr) (serverworks_private.gatt_pages[\
  100. GET_PAGE_DIR_IDX(addr)]->remapped)
  101. #ifndef GET_PAGE_DIR_OFF
  102. #define GET_PAGE_DIR_OFF(addr) (addr >> 22)
  103. #endif
  104. #ifndef GET_PAGE_DIR_IDX
  105. #define GET_PAGE_DIR_IDX(addr) (GET_PAGE_DIR_OFF(addr) - \
  106. GET_PAGE_DIR_OFF(agp_bridge->gart_bus_addr))
  107. #endif
  108. #ifndef GET_GATT_OFF
  109. #define GET_GATT_OFF(addr) ((addr & 0x003ff000) >> 12)
  110. #endif
  111. static int serverworks_create_gatt_table(struct agp_bridge_data *bridge)
  112. {
  113. struct aper_size_info_lvl2 *value;
  114. struct serverworks_page_map page_dir;
  115. int retval;
  116. u32 temp;
  117. int i;
  118. value = A_SIZE_LVL2(agp_bridge->current_size);
  119. retval = serverworks_create_page_map(&page_dir);
  120. if (retval != 0) {
  121. return retval;
  122. }
  123. retval = serverworks_create_page_map(&serverworks_private.scratch_dir);
  124. if (retval != 0) {
  125. serverworks_free_page_map(&page_dir);
  126. return retval;
  127. }
  128. /* Create a fake scratch directory */
  129. for (i = 0; i < 1024; i++) {
  130. writel(agp_bridge->scratch_page, serverworks_private.scratch_dir.remapped+i);
  131. writel(virt_to_phys(serverworks_private.scratch_dir.real) | 1, page_dir.remapped+i);
  132. }
  133. retval = serverworks_create_gatt_pages(value->num_entries / 1024);
  134. if (retval != 0) {
  135. serverworks_free_page_map(&page_dir);
  136. serverworks_free_page_map(&serverworks_private.scratch_dir);
  137. return retval;
  138. }
  139. agp_bridge->gatt_table_real = (u32 *)page_dir.real;
  140. agp_bridge->gatt_table = (u32 __iomem *)page_dir.remapped;
  141. agp_bridge->gatt_bus_addr = virt_to_phys(page_dir.real);
  142. /* Get the address for the gart region.
  143. * This is a bus address even on the alpha, b/c its
  144. * used to program the agp master not the cpu
  145. */
  146. pci_read_config_dword(agp_bridge->dev,serverworks_private.gart_addr_ofs,&temp);
  147. agp_bridge->gart_bus_addr = (temp & PCI_BASE_ADDRESS_MEM_MASK);
  148. /* Calculate the agp offset */
  149. for (i = 0; i < value->num_entries / 1024; i++)
  150. writel(virt_to_phys(serverworks_private.gatt_pages[i]->real)|1, page_dir.remapped+i);
  151. return 0;
  152. }
  153. static int serverworks_free_gatt_table(struct agp_bridge_data *bridge)
  154. {
  155. struct serverworks_page_map page_dir;
  156. page_dir.real = (unsigned long *)agp_bridge->gatt_table_real;
  157. page_dir.remapped = (unsigned long __iomem *)agp_bridge->gatt_table;
  158. serverworks_free_gatt_pages();
  159. serverworks_free_page_map(&page_dir);
  160. serverworks_free_page_map(&serverworks_private.scratch_dir);
  161. return 0;
  162. }
  163. static int serverworks_fetch_size(void)
  164. {
  165. int i;
  166. u32 temp;
  167. u32 temp2;
  168. struct aper_size_info_lvl2 *values;
  169. values = A_SIZE_LVL2(agp_bridge->driver->aperture_sizes);
  170. pci_read_config_dword(agp_bridge->dev,serverworks_private.gart_addr_ofs,&temp);
  171. pci_write_config_dword(agp_bridge->dev,serverworks_private.gart_addr_ofs,
  172. SVWRKS_SIZE_MASK);
  173. pci_read_config_dword(agp_bridge->dev,serverworks_private.gart_addr_ofs,&temp2);
  174. pci_write_config_dword(agp_bridge->dev,serverworks_private.gart_addr_ofs,temp);
  175. temp2 &= SVWRKS_SIZE_MASK;
  176. for (i = 0; i < agp_bridge->driver->num_aperture_sizes; i++) {
  177. if (temp2 == values[i].size_value) {
  178. agp_bridge->previous_size =
  179. agp_bridge->current_size = (void *) (values + i);
  180. agp_bridge->aperture_size_idx = i;
  181. return values[i].size;
  182. }
  183. }
  184. return 0;
  185. }
  186. /*
  187. * This routine could be implemented by taking the addresses
  188. * written to the GATT, and flushing them individually. However
  189. * currently it just flushes the whole table. Which is probably
  190. * more efficient, since agp_memory blocks can be a large number of
  191. * entries.
  192. */
  193. static void serverworks_tlbflush(struct agp_memory *temp)
  194. {
  195. unsigned long timeout;
  196. writeb(1, serverworks_private.registers+SVWRKS_POSTFLUSH);
  197. timeout = jiffies + 3*HZ;
  198. while (readb(serverworks_private.registers+SVWRKS_POSTFLUSH) == 1) {
  199. cpu_relax();
  200. if (time_after(jiffies, timeout)) {
  201. dev_err(&serverworks_private.svrwrks_dev->dev,
  202. "TLB post flush took more than 3 seconds\n");
  203. break;
  204. }
  205. }
  206. writel(1, serverworks_private.registers+SVWRKS_DIRFLUSH);
  207. timeout = jiffies + 3*HZ;
  208. while (readl(serverworks_private.registers+SVWRKS_DIRFLUSH) == 1) {
  209. cpu_relax();
  210. if (time_after(jiffies, timeout)) {
  211. dev_err(&serverworks_private.svrwrks_dev->dev,
  212. "TLB Dir flush took more than 3 seconds\n");
  213. break;
  214. }
  215. }
  216. }
  217. static int serverworks_configure(void)
  218. {
  219. struct aper_size_info_lvl2 *current_size;
  220. u32 temp;
  221. u8 enable_reg;
  222. u16 cap_reg;
  223. current_size = A_SIZE_LVL2(agp_bridge->current_size);
  224. /* Get the memory mapped registers */
  225. pci_read_config_dword(agp_bridge->dev, serverworks_private.mm_addr_ofs, &temp);
  226. temp = (temp & PCI_BASE_ADDRESS_MEM_MASK);
  227. serverworks_private.registers = (volatile u8 __iomem *) ioremap(temp, 4096);
  228. if (!serverworks_private.registers) {
  229. dev_err(&agp_bridge->dev->dev, "can't ioremap(%#x)\n", temp);
  230. return -ENOMEM;
  231. }
  232. writeb(0xA, serverworks_private.registers+SVWRKS_GART_CACHE);
  233. readb(serverworks_private.registers+SVWRKS_GART_CACHE); /* PCI Posting. */
  234. writel(agp_bridge->gatt_bus_addr, serverworks_private.registers+SVWRKS_GATTBASE);
  235. readl(serverworks_private.registers+SVWRKS_GATTBASE); /* PCI Posting. */
  236. cap_reg = readw(serverworks_private.registers+SVWRKS_COMMAND);
  237. cap_reg &= ~0x0007;
  238. cap_reg |= 0x4;
  239. writew(cap_reg, serverworks_private.registers+SVWRKS_COMMAND);
  240. readw(serverworks_private.registers+SVWRKS_COMMAND);
  241. pci_read_config_byte(serverworks_private.svrwrks_dev,SVWRKS_AGP_ENABLE, &enable_reg);
  242. enable_reg |= 0x1; /* Agp Enable bit */
  243. pci_write_config_byte(serverworks_private.svrwrks_dev,SVWRKS_AGP_ENABLE, enable_reg);
  244. serverworks_tlbflush(NULL);
  245. agp_bridge->capndx = pci_find_capability(serverworks_private.svrwrks_dev, PCI_CAP_ID_AGP);
  246. /* Fill in the mode register */
  247. pci_read_config_dword(serverworks_private.svrwrks_dev,
  248. agp_bridge->capndx+PCI_AGP_STATUS, &agp_bridge->mode);
  249. pci_read_config_byte(agp_bridge->dev, SVWRKS_CACHING, &enable_reg);
  250. enable_reg &= ~0x3;
  251. pci_write_config_byte(agp_bridge->dev, SVWRKS_CACHING, enable_reg);
  252. pci_read_config_byte(agp_bridge->dev, SVWRKS_FEATURE, &enable_reg);
  253. enable_reg |= (1<<6);
  254. pci_write_config_byte(agp_bridge->dev,SVWRKS_FEATURE, enable_reg);
  255. return 0;
  256. }
  257. static void serverworks_cleanup(void)
  258. {
  259. iounmap((void __iomem *) serverworks_private.registers);
  260. }
  261. static int serverworks_insert_memory(struct agp_memory *mem,
  262. off_t pg_start, int type)
  263. {
  264. int i, j, num_entries;
  265. unsigned long __iomem *cur_gatt;
  266. unsigned long addr;
  267. num_entries = A_SIZE_LVL2(agp_bridge->current_size)->num_entries;
  268. if (type != 0 || mem->type != 0) {
  269. return -EINVAL;
  270. }
  271. if ((pg_start + mem->page_count) > num_entries) {
  272. return -EINVAL;
  273. }
  274. j = pg_start;
  275. while (j < (pg_start + mem->page_count)) {
  276. addr = (j * PAGE_SIZE) + agp_bridge->gart_bus_addr;
  277. cur_gatt = SVRWRKS_GET_GATT(addr);
  278. if (!PGE_EMPTY(agp_bridge, readl(cur_gatt+GET_GATT_OFF(addr))))
  279. return -EBUSY;
  280. j++;
  281. }
  282. if (!mem->is_flushed) {
  283. global_cache_flush();
  284. mem->is_flushed = true;
  285. }
  286. for (i = 0, j = pg_start; i < mem->page_count; i++, j++) {
  287. addr = (j * PAGE_SIZE) + agp_bridge->gart_bus_addr;
  288. cur_gatt = SVRWRKS_GET_GATT(addr);
  289. writel(agp_bridge->driver->mask_memory(agp_bridge,
  290. page_to_phys(mem->pages[i]), mem->type),
  291. cur_gatt+GET_GATT_OFF(addr));
  292. }
  293. serverworks_tlbflush(mem);
  294. return 0;
  295. }
  296. static int serverworks_remove_memory(struct agp_memory *mem, off_t pg_start,
  297. int type)
  298. {
  299. int i;
  300. unsigned long __iomem *cur_gatt;
  301. unsigned long addr;
  302. if (type != 0 || mem->type != 0) {
  303. return -EINVAL;
  304. }
  305. global_cache_flush();
  306. serverworks_tlbflush(mem);
  307. for (i = pg_start; i < (mem->page_count + pg_start); i++) {
  308. addr = (i * PAGE_SIZE) + agp_bridge->gart_bus_addr;
  309. cur_gatt = SVRWRKS_GET_GATT(addr);
  310. writel(agp_bridge->scratch_page, cur_gatt+GET_GATT_OFF(addr));
  311. }
  312. serverworks_tlbflush(mem);
  313. return 0;
  314. }
  315. static const struct gatt_mask serverworks_masks[] =
  316. {
  317. {.mask = 1, .type = 0}
  318. };
  319. static const struct aper_size_info_lvl2 serverworks_sizes[7] =
  320. {
  321. {2048, 524288, 0x80000000},
  322. {1024, 262144, 0xc0000000},
  323. {512, 131072, 0xe0000000},
  324. {256, 65536, 0xf0000000},
  325. {128, 32768, 0xf8000000},
  326. {64, 16384, 0xfc000000},
  327. {32, 8192, 0xfe000000}
  328. };
  329. static void serverworks_agp_enable(struct agp_bridge_data *bridge, u32 mode)
  330. {
  331. u32 command;
  332. pci_read_config_dword(serverworks_private.svrwrks_dev,
  333. bridge->capndx + PCI_AGP_STATUS,
  334. &command);
  335. command = agp_collect_device_status(bridge, mode, command);
  336. command &= ~0x10; /* disable FW */
  337. command &= ~0x08;
  338. command |= 0x100;
  339. pci_write_config_dword(serverworks_private.svrwrks_dev,
  340. bridge->capndx + PCI_AGP_COMMAND,
  341. command);
  342. agp_device_command(command, false);
  343. }
  344. static const struct agp_bridge_driver sworks_driver = {
  345. .owner = THIS_MODULE,
  346. .aperture_sizes = serverworks_sizes,
  347. .size_type = LVL2_APER_SIZE,
  348. .num_aperture_sizes = 7,
  349. .configure = serverworks_configure,
  350. .fetch_size = serverworks_fetch_size,
  351. .cleanup = serverworks_cleanup,
  352. .tlb_flush = serverworks_tlbflush,
  353. .mask_memory = agp_generic_mask_memory,
  354. .masks = serverworks_masks,
  355. .agp_enable = serverworks_agp_enable,
  356. .cache_flush = global_cache_flush,
  357. .create_gatt_table = serverworks_create_gatt_table,
  358. .free_gatt_table = serverworks_free_gatt_table,
  359. .insert_memory = serverworks_insert_memory,
  360. .remove_memory = serverworks_remove_memory,
  361. .alloc_by_type = agp_generic_alloc_by_type,
  362. .free_by_type = agp_generic_free_by_type,
  363. .agp_alloc_page = agp_generic_alloc_page,
  364. .agp_alloc_pages = agp_generic_alloc_pages,
  365. .agp_destroy_page = agp_generic_destroy_page,
  366. .agp_destroy_pages = agp_generic_destroy_pages,
  367. .agp_type_to_mask_type = agp_generic_type_to_mask_type,
  368. };
  369. static int agp_serverworks_probe(struct pci_dev *pdev,
  370. const struct pci_device_id *ent)
  371. {
  372. struct agp_bridge_data *bridge;
  373. struct pci_dev *bridge_dev;
  374. u32 temp, temp2;
  375. u8 cap_ptr = 0;
  376. cap_ptr = pci_find_capability(pdev, PCI_CAP_ID_AGP);
  377. switch (pdev->device) {
  378. case 0x0006:
  379. dev_err(&pdev->dev, "ServerWorks CNB20HE is unsupported due to lack of documentation\n");
  380. return -ENODEV;
  381. case PCI_DEVICE_ID_SERVERWORKS_HE:
  382. case PCI_DEVICE_ID_SERVERWORKS_LE:
  383. case 0x0007:
  384. break;
  385. default:
  386. if (cap_ptr)
  387. dev_err(&pdev->dev, "unsupported Serverworks chipset "
  388. "[%04x/%04x]\n", pdev->vendor, pdev->device);
  389. return -ENODEV;
  390. }
  391. /* Everything is on func 1 here so we are hardcoding function one */
  392. bridge_dev = pci_get_bus_and_slot((unsigned int)pdev->bus->number,
  393. PCI_DEVFN(0, 1));
  394. if (!bridge_dev) {
  395. dev_info(&pdev->dev, "can't find secondary device\n");
  396. return -ENODEV;
  397. }
  398. serverworks_private.svrwrks_dev = bridge_dev;
  399. serverworks_private.gart_addr_ofs = 0x10;
  400. pci_read_config_dword(pdev, SVWRKS_APSIZE, &temp);
  401. if (temp & PCI_BASE_ADDRESS_MEM_TYPE_64) {
  402. pci_read_config_dword(pdev, SVWRKS_APSIZE + 4, &temp2);
  403. if (temp2 != 0) {
  404. dev_info(&pdev->dev, "64 bit aperture address, "
  405. "but top bits are not zero; disabling AGP\n");
  406. return -ENODEV;
  407. }
  408. serverworks_private.mm_addr_ofs = 0x18;
  409. } else
  410. serverworks_private.mm_addr_ofs = 0x14;
  411. pci_read_config_dword(pdev, serverworks_private.mm_addr_ofs, &temp);
  412. if (temp & PCI_BASE_ADDRESS_MEM_TYPE_64) {
  413. pci_read_config_dword(pdev,
  414. serverworks_private.mm_addr_ofs + 4, &temp2);
  415. if (temp2 != 0) {
  416. dev_info(&pdev->dev, "64 bit MMIO address, but top "
  417. "bits are not zero; disabling AGP\n");
  418. return -ENODEV;
  419. }
  420. }
  421. bridge = agp_alloc_bridge();
  422. if (!bridge)
  423. return -ENOMEM;
  424. bridge->driver = &sworks_driver;
  425. bridge->dev_private_data = &serverworks_private,
  426. bridge->dev = pci_dev_get(pdev);
  427. pci_set_drvdata(pdev, bridge);
  428. return agp_add_bridge(bridge);
  429. }
  430. static void agp_serverworks_remove(struct pci_dev *pdev)
  431. {
  432. struct agp_bridge_data *bridge = pci_get_drvdata(pdev);
  433. pci_dev_put(bridge->dev);
  434. agp_remove_bridge(bridge);
  435. agp_put_bridge(bridge);
  436. pci_dev_put(serverworks_private.svrwrks_dev);
  437. serverworks_private.svrwrks_dev = NULL;
  438. }
  439. static struct pci_device_id agp_serverworks_pci_table[] = {
  440. {
  441. .class = (PCI_CLASS_BRIDGE_HOST << 8),
  442. .class_mask = ~0,
  443. .vendor = PCI_VENDOR_ID_SERVERWORKS,
  444. .device = PCI_ANY_ID,
  445. .subvendor = PCI_ANY_ID,
  446. .subdevice = PCI_ANY_ID,
  447. },
  448. { }
  449. };
  450. MODULE_DEVICE_TABLE(pci, agp_serverworks_pci_table);
  451. static struct pci_driver agp_serverworks_pci_driver = {
  452. .name = "agpgart-serverworks",
  453. .id_table = agp_serverworks_pci_table,
  454. .probe = agp_serverworks_probe,
  455. .remove = agp_serverworks_remove,
  456. };
  457. static int __init agp_serverworks_init(void)
  458. {
  459. if (agp_off)
  460. return -EINVAL;
  461. return pci_register_driver(&agp_serverworks_pci_driver);
  462. }
  463. static void __exit agp_serverworks_cleanup(void)
  464. {
  465. pci_unregister_driver(&agp_serverworks_pci_driver);
  466. }
  467. module_init(agp_serverworks_init);
  468. module_exit(agp_serverworks_cleanup);
  469. MODULE_LICENSE("GPL and additional rights");