tioce_provider.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 2003-2005 Silicon Graphics, Inc. All Rights Reserved.
  7. */
  8. #include <linux/types.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/pci.h>
  11. #include <asm/sn/sn_sal.h>
  12. #include <asm/sn/addrs.h>
  13. #include <asm/sn/io.h>
  14. #include <asm/sn/pcidev.h>
  15. #include <asm/sn/pcibus_provider_defs.h>
  16. #include <asm/sn/tioce_provider.h>
  17. /**
  18. * Bus address ranges for the 5 flavors of TIOCE DMA
  19. */
  20. #define TIOCE_D64_MIN 0x8000000000000000UL
  21. #define TIOCE_D64_MAX 0xffffffffffffffffUL
  22. #define TIOCE_D64_ADDR(a) ((a) >= TIOCE_D64_MIN)
  23. #define TIOCE_D32_MIN 0x0000000080000000UL
  24. #define TIOCE_D32_MAX 0x00000000ffffffffUL
  25. #define TIOCE_D32_ADDR(a) ((a) >= TIOCE_D32_MIN && (a) <= TIOCE_D32_MAX)
  26. #define TIOCE_M32_MIN 0x0000000000000000UL
  27. #define TIOCE_M32_MAX 0x000000007fffffffUL
  28. #define TIOCE_M32_ADDR(a) ((a) >= TIOCE_M32_MIN && (a) <= TIOCE_M32_MAX)
  29. #define TIOCE_M40_MIN 0x0000004000000000UL
  30. #define TIOCE_M40_MAX 0x0000007fffffffffUL
  31. #define TIOCE_M40_ADDR(a) ((a) >= TIOCE_M40_MIN && (a) <= TIOCE_M40_MAX)
  32. #define TIOCE_M40S_MIN 0x0000008000000000UL
  33. #define TIOCE_M40S_MAX 0x000000ffffffffffUL
  34. #define TIOCE_M40S_ADDR(a) ((a) >= TIOCE_M40S_MIN && (a) <= TIOCE_M40S_MAX)
  35. /*
  36. * ATE manipulation macros.
  37. */
  38. #define ATE_PAGESHIFT(ps) (__ffs(ps))
  39. #define ATE_PAGEMASK(ps) ((ps)-1)
  40. #define ATE_PAGE(x, ps) ((x) >> ATE_PAGESHIFT(ps))
  41. #define ATE_NPAGES(start, len, pagesize) \
  42. (ATE_PAGE((start)+(len)-1, pagesize) - ATE_PAGE(start, pagesize) + 1)
  43. #define ATE_VALID(ate) ((ate) & (1UL << 63))
  44. #define ATE_MAKE(addr, ps) (((addr) & ~ATE_PAGEMASK(ps)) | (1UL << 63))
  45. /*
  46. * Flavors of ate-based mapping supported by tioce_alloc_map()
  47. */
  48. #define TIOCE_ATE_M32 1
  49. #define TIOCE_ATE_M40 2
  50. #define TIOCE_ATE_M40S 3
  51. #define KB(x) ((x) << 10)
  52. #define MB(x) ((x) << 20)
  53. #define GB(x) ((x) << 30)
  54. /**
  55. * tioce_dma_d64 - create a DMA mapping using 64-bit direct mode
  56. * @ct_addr: system coretalk address
  57. *
  58. * Map @ct_addr into 64-bit CE bus space. No device context is necessary
  59. * and no CE mapping are consumed.
  60. *
  61. * Bits 53:0 come from the coretalk address. The remaining bits are set as
  62. * follows:
  63. *
  64. * 63 - must be 1 to indicate d64 mode to CE hardware
  65. * 62 - barrier bit ... controlled with tioce_dma_barrier()
  66. * 61 - 0 since this is not an MSI transaction
  67. * 60:54 - reserved, MBZ
  68. */
  69. static u64
  70. tioce_dma_d64(unsigned long ct_addr)
  71. {
  72. u64 bus_addr;
  73. bus_addr = ct_addr | (1UL << 63);
  74. return bus_addr;
  75. }
  76. /**
  77. * pcidev_to_tioce - return misc ce related pointers given a pci_dev
  78. * @pci_dev: pci device context
  79. * @base: ptr to store struct tioce_mmr * for the CE holding this device
  80. * @kernel: ptr to store struct tioce_kernel * for the CE holding this device
  81. * @port: ptr to store the CE port number that this device is on
  82. *
  83. * Return pointers to various CE-related structures for the CE upstream of
  84. * @pci_dev.
  85. */
  86. static inline void
  87. pcidev_to_tioce(struct pci_dev *pdev, struct tioce **base,
  88. struct tioce_kernel **kernel, int *port)
  89. {
  90. struct pcidev_info *pcidev_info;
  91. struct tioce_common *ce_common;
  92. struct tioce_kernel *ce_kernel;
  93. pcidev_info = SN_PCIDEV_INFO(pdev);
  94. ce_common = (struct tioce_common *)pcidev_info->pdi_pcibus_info;
  95. ce_kernel = (struct tioce_kernel *)ce_common->ce_kernel_private;
  96. if (base)
  97. *base = (struct tioce *)ce_common->ce_pcibus.bs_base;
  98. if (kernel)
  99. *kernel = ce_kernel;
  100. /*
  101. * we use port as a zero-based value internally, even though the
  102. * documentation is 1-based.
  103. */
  104. if (port)
  105. *port =
  106. (pdev->bus->number < ce_kernel->ce_port1_secondary) ? 0 : 1;
  107. }
  108. /**
  109. * tioce_alloc_map - Given a coretalk address, map it to pcie bus address
  110. * space using one of the various ATE-based address modes.
  111. * @ce_kern: tioce context
  112. * @type: map mode to use
  113. * @port: 0-based port that the requesting device is downstream of
  114. * @ct_addr: the coretalk address to map
  115. * @len: number of bytes to map
  116. *
  117. * Given the addressing type, set up various paramaters that define the
  118. * ATE pool to use. Search for a contiguous block of entries to cover the
  119. * length, and if enough resources exist, fill in the ATE's and construct a
  120. * tioce_dmamap struct to track the mapping.
  121. */
  122. static u64
  123. tioce_alloc_map(struct tioce_kernel *ce_kern, int type, int port,
  124. u64 ct_addr, int len)
  125. {
  126. int i;
  127. int j;
  128. int first;
  129. int last;
  130. int entries;
  131. int nates;
  132. int pagesize;
  133. u64 *ate_shadow;
  134. u64 *ate_reg;
  135. u64 addr;
  136. struct tioce *ce_mmr;
  137. u64 bus_base;
  138. struct tioce_dmamap *map;
  139. ce_mmr = (struct tioce *)ce_kern->ce_common->ce_pcibus.bs_base;
  140. switch (type) {
  141. case TIOCE_ATE_M32:
  142. /*
  143. * The first 64 entries of the ate3240 pool are dedicated to
  144. * super-page (TIOCE_ATE_M40S) mode.
  145. */
  146. first = 64;
  147. entries = TIOCE_NUM_M3240_ATES - 64;
  148. ate_shadow = ce_kern->ce_ate3240_shadow;
  149. ate_reg = ce_mmr->ce_ure_ate3240;
  150. pagesize = ce_kern->ce_ate3240_pagesize;
  151. bus_base = TIOCE_M32_MIN;
  152. break;
  153. case TIOCE_ATE_M40:
  154. first = 0;
  155. entries = TIOCE_NUM_M40_ATES;
  156. ate_shadow = ce_kern->ce_ate40_shadow;
  157. ate_reg = ce_mmr->ce_ure_ate40;
  158. pagesize = MB(64);
  159. bus_base = TIOCE_M40_MIN;
  160. break;
  161. case TIOCE_ATE_M40S:
  162. /*
  163. * ate3240 entries 0-31 are dedicated to port1 super-page
  164. * mappings. ate3240 entries 32-63 are dedicated to port2.
  165. */
  166. first = port * 32;
  167. entries = 32;
  168. ate_shadow = ce_kern->ce_ate3240_shadow;
  169. ate_reg = ce_mmr->ce_ure_ate3240;
  170. pagesize = GB(16);
  171. bus_base = TIOCE_M40S_MIN;
  172. break;
  173. default:
  174. return 0;
  175. }
  176. nates = ATE_NPAGES(ct_addr, len, pagesize);
  177. if (nates > entries)
  178. return 0;
  179. last = first + entries - nates;
  180. for (i = first; i <= last; i++) {
  181. if (ATE_VALID(ate_shadow[i]))
  182. continue;
  183. for (j = i; j < i + nates; j++)
  184. if (ATE_VALID(ate_shadow[j]))
  185. break;
  186. if (j >= i + nates)
  187. break;
  188. }
  189. if (i > last)
  190. return 0;
  191. map = kzalloc(sizeof(struct tioce_dmamap), GFP_ATOMIC);
  192. if (!map)
  193. return 0;
  194. addr = ct_addr;
  195. for (j = 0; j < nates; j++) {
  196. u64 ate;
  197. ate = ATE_MAKE(addr, pagesize);
  198. ate_shadow[i + j] = ate;
  199. writeq(ate, &ate_reg[i + j]);
  200. addr += pagesize;
  201. }
  202. map->refcnt = 1;
  203. map->nbytes = nates * pagesize;
  204. map->ct_start = ct_addr & ~ATE_PAGEMASK(pagesize);
  205. map->pci_start = bus_base + (i * pagesize);
  206. map->ate_hw = &ate_reg[i];
  207. map->ate_shadow = &ate_shadow[i];
  208. map->ate_count = nates;
  209. list_add(&map->ce_dmamap_list, &ce_kern->ce_dmamap_list);
  210. return (map->pci_start + (ct_addr - map->ct_start));
  211. }
  212. /**
  213. * tioce_dma_d32 - create a DMA mapping using 32-bit direct mode
  214. * @pdev: linux pci_dev representing the function
  215. * @paddr: system physical address
  216. *
  217. * Map @paddr into 32-bit bus space of the CE associated with @pcidev_info.
  218. */
  219. static u64
  220. tioce_dma_d32(struct pci_dev *pdev, u64 ct_addr)
  221. {
  222. int dma_ok;
  223. int port;
  224. struct tioce *ce_mmr;
  225. struct tioce_kernel *ce_kern;
  226. u64 ct_upper;
  227. u64 ct_lower;
  228. dma_addr_t bus_addr;
  229. ct_upper = ct_addr & ~0x3fffffffUL;
  230. ct_lower = ct_addr & 0x3fffffffUL;
  231. pcidev_to_tioce(pdev, &ce_mmr, &ce_kern, &port);
  232. if (ce_kern->ce_port[port].dirmap_refcnt == 0) {
  233. u64 tmp;
  234. ce_kern->ce_port[port].dirmap_shadow = ct_upper;
  235. writeq(ct_upper, &ce_mmr->ce_ure_dir_map[port]);
  236. tmp = ce_mmr->ce_ure_dir_map[port];
  237. dma_ok = 1;
  238. } else
  239. dma_ok = (ce_kern->ce_port[port].dirmap_shadow == ct_upper);
  240. if (dma_ok) {
  241. ce_kern->ce_port[port].dirmap_refcnt++;
  242. bus_addr = TIOCE_D32_MIN + ct_lower;
  243. } else
  244. bus_addr = 0;
  245. return bus_addr;
  246. }
  247. /**
  248. * tioce_dma_barrier - swizzle a TIOCE bus address to include or exclude
  249. * the barrier bit.
  250. * @bus_addr: bus address to swizzle
  251. *
  252. * Given a TIOCE bus address, set the appropriate bit to indicate barrier
  253. * attributes.
  254. */
  255. static u64
  256. tioce_dma_barrier(u64 bus_addr, int on)
  257. {
  258. u64 barrier_bit;
  259. /* barrier not supported in M40/M40S mode */
  260. if (TIOCE_M40_ADDR(bus_addr) || TIOCE_M40S_ADDR(bus_addr))
  261. return bus_addr;
  262. if (TIOCE_D64_ADDR(bus_addr))
  263. barrier_bit = (1UL << 62);
  264. else /* must be m32 or d32 */
  265. barrier_bit = (1UL << 30);
  266. return (on) ? (bus_addr | barrier_bit) : (bus_addr & ~barrier_bit);
  267. }
  268. /**
  269. * tioce_dma_unmap - release CE mapping resources
  270. * @pdev: linux pci_dev representing the function
  271. * @bus_addr: bus address returned by an earlier tioce_dma_map
  272. * @dir: mapping direction (unused)
  273. *
  274. * Locate mapping resources associated with @bus_addr and release them.
  275. * For mappings created using the direct modes there are no resources
  276. * to release.
  277. */
  278. void
  279. tioce_dma_unmap(struct pci_dev *pdev, dma_addr_t bus_addr, int dir)
  280. {
  281. int i;
  282. int port;
  283. struct tioce_kernel *ce_kern;
  284. struct tioce *ce_mmr;
  285. unsigned long flags;
  286. bus_addr = tioce_dma_barrier(bus_addr, 0);
  287. pcidev_to_tioce(pdev, &ce_mmr, &ce_kern, &port);
  288. /* nothing to do for D64 */
  289. if (TIOCE_D64_ADDR(bus_addr))
  290. return;
  291. spin_lock_irqsave(&ce_kern->ce_lock, flags);
  292. if (TIOCE_D32_ADDR(bus_addr)) {
  293. if (--ce_kern->ce_port[port].dirmap_refcnt == 0) {
  294. ce_kern->ce_port[port].dirmap_shadow = 0;
  295. writeq(0, &ce_mmr->ce_ure_dir_map[port]);
  296. }
  297. } else {
  298. struct tioce_dmamap *map;
  299. list_for_each_entry(map, &ce_kern->ce_dmamap_list,
  300. ce_dmamap_list) {
  301. u64 last;
  302. last = map->pci_start + map->nbytes - 1;
  303. if (bus_addr >= map->pci_start && bus_addr <= last)
  304. break;
  305. }
  306. if (&map->ce_dmamap_list == &ce_kern->ce_dmamap_list) {
  307. printk(KERN_WARNING
  308. "%s: %s - no map found for bus_addr 0x%lx\n",
  309. __FUNCTION__, pci_name(pdev), bus_addr);
  310. } else if (--map->refcnt == 0) {
  311. for (i = 0; i < map->ate_count; i++) {
  312. map->ate_shadow[i] = 0;
  313. map->ate_hw[i] = 0;
  314. }
  315. list_del(&map->ce_dmamap_list);
  316. kfree(map);
  317. }
  318. }
  319. spin_unlock_irqrestore(&ce_kern->ce_lock, flags);
  320. }
  321. /**
  322. * tioce_do_dma_map - map pages for PCI DMA
  323. * @pdev: linux pci_dev representing the function
  324. * @paddr: host physical address to map
  325. * @byte_count: bytes to map
  326. *
  327. * This is the main wrapper for mapping host physical pages to CE PCI space.
  328. * The mapping mode used is based on the device's dma_mask.
  329. */
  330. static u64
  331. tioce_do_dma_map(struct pci_dev *pdev, u64 paddr, size_t byte_count,
  332. int barrier)
  333. {
  334. unsigned long flags;
  335. u64 ct_addr;
  336. u64 mapaddr = 0;
  337. struct tioce_kernel *ce_kern;
  338. struct tioce_dmamap *map;
  339. int port;
  340. u64 dma_mask;
  341. dma_mask = (barrier) ? pdev->dev.coherent_dma_mask : pdev->dma_mask;
  342. /* cards must be able to address at least 31 bits */
  343. if (dma_mask < 0x7fffffffUL)
  344. return 0;
  345. ct_addr = PHYS_TO_TIODMA(paddr);
  346. /*
  347. * If the device can generate 64 bit addresses, create a D64 map.
  348. * Since this should never fail, bypass the rest of the checks.
  349. */
  350. if (dma_mask == ~0UL) {
  351. mapaddr = tioce_dma_d64(ct_addr);
  352. goto dma_map_done;
  353. }
  354. pcidev_to_tioce(pdev, NULL, &ce_kern, &port);
  355. spin_lock_irqsave(&ce_kern->ce_lock, flags);
  356. /*
  357. * D64 didn't work ... See if we have an existing map that covers
  358. * this address range. Must account for devices dma_mask here since
  359. * an existing map might have been done in a mode using more pci
  360. * address bits than this device can support.
  361. */
  362. list_for_each_entry(map, &ce_kern->ce_dmamap_list, ce_dmamap_list) {
  363. u64 last;
  364. last = map->ct_start + map->nbytes - 1;
  365. if (ct_addr >= map->ct_start &&
  366. ct_addr + byte_count - 1 <= last &&
  367. map->pci_start <= dma_mask) {
  368. map->refcnt++;
  369. mapaddr = map->pci_start + (ct_addr - map->ct_start);
  370. break;
  371. }
  372. }
  373. /*
  374. * If we don't have a map yet, and the card can generate 40
  375. * bit addresses, try the M40/M40S modes. Note these modes do not
  376. * support a barrier bit, so if we need a consistent map these
  377. * won't work.
  378. */
  379. if (!mapaddr && !barrier && dma_mask >= 0xffffffffffUL) {
  380. /*
  381. * We have two options for 40-bit mappings: 16GB "super" ATE's
  382. * and 64MB "regular" ATE's. We'll try both if needed for a
  383. * given mapping but which one we try first depends on the
  384. * size. For requests >64MB, prefer to use a super page with
  385. * regular as the fallback. Otherwise, try in the reverse order.
  386. */
  387. if (byte_count > MB(64)) {
  388. mapaddr = tioce_alloc_map(ce_kern, TIOCE_ATE_M40S,
  389. port, ct_addr, byte_count);
  390. if (!mapaddr)
  391. mapaddr =
  392. tioce_alloc_map(ce_kern, TIOCE_ATE_M40, -1,
  393. ct_addr, byte_count);
  394. } else {
  395. mapaddr = tioce_alloc_map(ce_kern, TIOCE_ATE_M40, -1,
  396. ct_addr, byte_count);
  397. if (!mapaddr)
  398. mapaddr =
  399. tioce_alloc_map(ce_kern, TIOCE_ATE_M40S,
  400. port, ct_addr, byte_count);
  401. }
  402. }
  403. /*
  404. * 32-bit direct is the next mode to try
  405. */
  406. if (!mapaddr && dma_mask >= 0xffffffffUL)
  407. mapaddr = tioce_dma_d32(pdev, ct_addr);
  408. /*
  409. * Last resort, try 32-bit ATE-based map.
  410. */
  411. if (!mapaddr)
  412. mapaddr =
  413. tioce_alloc_map(ce_kern, TIOCE_ATE_M32, -1, ct_addr,
  414. byte_count);
  415. spin_unlock_irqrestore(&ce_kern->ce_lock, flags);
  416. dma_map_done:
  417. if (mapaddr & barrier)
  418. mapaddr = tioce_dma_barrier(mapaddr, 1);
  419. return mapaddr;
  420. }
  421. /**
  422. * tioce_dma - standard pci dma map interface
  423. * @pdev: pci device requesting the map
  424. * @paddr: system physical address to map into pci space
  425. * @byte_count: # bytes to map
  426. *
  427. * Simply call tioce_do_dma_map() to create a map with the barrier bit clear
  428. * in the address.
  429. */
  430. static u64
  431. tioce_dma(struct pci_dev *pdev, u64 paddr, size_t byte_count)
  432. {
  433. return tioce_do_dma_map(pdev, paddr, byte_count, 0);
  434. }
  435. /**
  436. * tioce_dma_consistent - consistent pci dma map interface
  437. * @pdev: pci device requesting the map
  438. * @paddr: system physical address to map into pci space
  439. * @byte_count: # bytes to map
  440. *
  441. * Simply call tioce_do_dma_map() to create a map with the barrier bit set
  442. * in the address.
  443. */ static u64
  444. tioce_dma_consistent(struct pci_dev *pdev, u64 paddr, size_t byte_count)
  445. {
  446. return tioce_do_dma_map(pdev, paddr, byte_count, 1);
  447. }
  448. /**
  449. * tioce_error_intr_handler - SGI TIO CE error interrupt handler
  450. * @irq: unused
  451. * @arg: pointer to tioce_common struct for the given CE
  452. * @pt: unused
  453. *
  454. * Handle a CE error interrupt. Simply a wrapper around a SAL call which
  455. * defers processing to the SGI prom.
  456. */ static irqreturn_t
  457. tioce_error_intr_handler(int irq, void *arg, struct pt_regs *pt)
  458. {
  459. struct tioce_common *soft = arg;
  460. struct ia64_sal_retval ret_stuff;
  461. ret_stuff.status = 0;
  462. ret_stuff.v0 = 0;
  463. SAL_CALL_NOLOCK(ret_stuff, (u64) SN_SAL_IOIF_ERROR_INTERRUPT,
  464. soft->ce_pcibus.bs_persist_segment,
  465. soft->ce_pcibus.bs_persist_busnum, 0, 0, 0, 0, 0);
  466. return IRQ_HANDLED;
  467. }
  468. /**
  469. * tioce_kern_init - init kernel structures related to a given TIOCE
  470. * @tioce_common: ptr to a cached tioce_common struct that originated in prom
  471. */ static struct tioce_kernel *
  472. tioce_kern_init(struct tioce_common *tioce_common)
  473. {
  474. int i;
  475. u32 tmp;
  476. struct tioce *tioce_mmr;
  477. struct tioce_kernel *tioce_kern;
  478. tioce_kern = kzalloc(sizeof(struct tioce_kernel), GFP_KERNEL);
  479. if (!tioce_kern) {
  480. return NULL;
  481. }
  482. tioce_kern->ce_common = tioce_common;
  483. spin_lock_init(&tioce_kern->ce_lock);
  484. INIT_LIST_HEAD(&tioce_kern->ce_dmamap_list);
  485. tioce_common->ce_kernel_private = (u64) tioce_kern;
  486. /*
  487. * Determine the secondary bus number of the port2 logical PPB.
  488. * This is used to decide whether a given pci device resides on
  489. * port1 or port2. Note: We don't have enough plumbing set up
  490. * here to use pci_read_config_xxx() so use the raw_pci_ops vector.
  491. */
  492. raw_pci_ops->read(tioce_common->ce_pcibus.bs_persist_segment,
  493. tioce_common->ce_pcibus.bs_persist_busnum,
  494. PCI_DEVFN(2, 0), PCI_SECONDARY_BUS, 1, &tmp);
  495. tioce_kern->ce_port1_secondary = (u8) tmp;
  496. /*
  497. * Set PMU pagesize to the largest size available, and zero out
  498. * the ate's.
  499. */
  500. tioce_mmr = (struct tioce *)tioce_common->ce_pcibus.bs_base;
  501. __sn_clrq_relaxed(&tioce_mmr->ce_ure_page_map, CE_URE_PAGESIZE_MASK);
  502. __sn_setq_relaxed(&tioce_mmr->ce_ure_page_map, CE_URE_256K_PAGESIZE);
  503. tioce_kern->ce_ate3240_pagesize = KB(256);
  504. for (i = 0; i < TIOCE_NUM_M40_ATES; i++) {
  505. tioce_kern->ce_ate40_shadow[i] = 0;
  506. writeq(0, &tioce_mmr->ce_ure_ate40[i]);
  507. }
  508. for (i = 0; i < TIOCE_NUM_M3240_ATES; i++) {
  509. tioce_kern->ce_ate3240_shadow[i] = 0;
  510. writeq(0, &tioce_mmr->ce_ure_ate3240[i]);
  511. }
  512. return tioce_kern;
  513. }
  514. /**
  515. * tioce_force_interrupt - implement altix force_interrupt() backend for CE
  516. * @sn_irq_info: sn asic irq that we need an interrupt generated for
  517. *
  518. * Given an sn_irq_info struct, set the proper bit in ce_adm_force_int to
  519. * force a secondary interrupt to be generated. This is to work around an
  520. * asic issue where there is a small window of opportunity for a legacy device
  521. * interrupt to be lost.
  522. */
  523. static void
  524. tioce_force_interrupt(struct sn_irq_info *sn_irq_info)
  525. {
  526. struct pcidev_info *pcidev_info;
  527. struct tioce_common *ce_common;
  528. struct tioce *ce_mmr;
  529. u64 force_int_val;
  530. if (!sn_irq_info->irq_bridge)
  531. return;
  532. if (sn_irq_info->irq_bridge_type != PCIIO_ASIC_TYPE_TIOCE)
  533. return;
  534. pcidev_info = (struct pcidev_info *)sn_irq_info->irq_pciioinfo;
  535. if (!pcidev_info)
  536. return;
  537. ce_common = (struct tioce_common *)pcidev_info->pdi_pcibus_info;
  538. ce_mmr = (struct tioce *)ce_common->ce_pcibus.bs_base;
  539. /*
  540. * irq_int_bit is originally set up by prom, and holds the interrupt
  541. * bit shift (not mask) as defined by the bit definitions in the
  542. * ce_adm_int mmr. These shifts are not the same for the
  543. * ce_adm_force_int register, so do an explicit mapping here to make
  544. * things clearer.
  545. */
  546. switch (sn_irq_info->irq_int_bit) {
  547. case CE_ADM_INT_PCIE_PORT1_DEV_A_SHFT:
  548. force_int_val = 1UL << CE_ADM_FORCE_INT_PCIE_PORT1_DEV_A_SHFT;
  549. break;
  550. case CE_ADM_INT_PCIE_PORT1_DEV_B_SHFT:
  551. force_int_val = 1UL << CE_ADM_FORCE_INT_PCIE_PORT1_DEV_B_SHFT;
  552. break;
  553. case CE_ADM_INT_PCIE_PORT1_DEV_C_SHFT:
  554. force_int_val = 1UL << CE_ADM_FORCE_INT_PCIE_PORT1_DEV_C_SHFT;
  555. break;
  556. case CE_ADM_INT_PCIE_PORT1_DEV_D_SHFT:
  557. force_int_val = 1UL << CE_ADM_FORCE_INT_PCIE_PORT1_DEV_D_SHFT;
  558. break;
  559. case CE_ADM_INT_PCIE_PORT2_DEV_A_SHFT:
  560. force_int_val = 1UL << CE_ADM_FORCE_INT_PCIE_PORT2_DEV_A_SHFT;
  561. break;
  562. case CE_ADM_INT_PCIE_PORT2_DEV_B_SHFT:
  563. force_int_val = 1UL << CE_ADM_FORCE_INT_PCIE_PORT2_DEV_B_SHFT;
  564. break;
  565. case CE_ADM_INT_PCIE_PORT2_DEV_C_SHFT:
  566. force_int_val = 1UL << CE_ADM_FORCE_INT_PCIE_PORT2_DEV_C_SHFT;
  567. break;
  568. case CE_ADM_INT_PCIE_PORT2_DEV_D_SHFT:
  569. force_int_val = 1UL << CE_ADM_FORCE_INT_PCIE_PORT2_DEV_D_SHFT;
  570. break;
  571. default:
  572. return;
  573. }
  574. writeq(force_int_val, &ce_mmr->ce_adm_force_int);
  575. }
  576. /**
  577. * tioce_target_interrupt - implement set_irq_affinity for tioce resident
  578. * functions. Note: only applies to line interrupts, not MSI's.
  579. *
  580. * @sn_irq_info: SN IRQ context
  581. *
  582. * Given an sn_irq_info, set the associated CE device's interrupt destination
  583. * register. Since the interrupt destination registers are on a per-ce-slot
  584. * basis, this will retarget line interrupts for all functions downstream of
  585. * the slot.
  586. */
  587. static void
  588. tioce_target_interrupt(struct sn_irq_info *sn_irq_info)
  589. {
  590. struct pcidev_info *pcidev_info;
  591. struct tioce_common *ce_common;
  592. struct tioce *ce_mmr;
  593. int bit;
  594. u64 vector;
  595. pcidev_info = (struct pcidev_info *)sn_irq_info->irq_pciioinfo;
  596. if (!pcidev_info)
  597. return;
  598. ce_common = (struct tioce_common *)pcidev_info->pdi_pcibus_info;
  599. ce_mmr = (struct tioce *)ce_common->ce_pcibus.bs_base;
  600. bit = sn_irq_info->irq_int_bit;
  601. __sn_setq_relaxed(&ce_mmr->ce_adm_int_mask, (1UL << bit));
  602. vector = (u64)sn_irq_info->irq_irq << INTR_VECTOR_SHFT;
  603. vector |= sn_irq_info->irq_xtalkaddr;
  604. writeq(vector, &ce_mmr->ce_adm_int_dest[bit]);
  605. __sn_clrq_relaxed(&ce_mmr->ce_adm_int_mask, (1UL << bit));
  606. tioce_force_interrupt(sn_irq_info);
  607. }
  608. /**
  609. * tioce_bus_fixup - perform final PCI fixup for a TIO CE bus
  610. * @prom_bussoft: Common prom/kernel struct representing the bus
  611. *
  612. * Replicates the tioce_common pointed to by @prom_bussoft in kernel
  613. * space. Allocates and initializes a kernel-only area for a given CE,
  614. * and sets up an irq for handling CE error interrupts.
  615. *
  616. * On successful setup, returns the kernel version of tioce_common back to
  617. * the caller.
  618. */
  619. static void *
  620. tioce_bus_fixup(struct pcibus_bussoft *prom_bussoft, struct pci_controller *controller)
  621. {
  622. struct tioce_common *tioce_common;
  623. /*
  624. * Allocate kernel bus soft and copy from prom.
  625. */
  626. tioce_common = kzalloc(sizeof(struct tioce_common), GFP_KERNEL);
  627. if (!tioce_common)
  628. return NULL;
  629. memcpy(tioce_common, prom_bussoft, sizeof(struct tioce_common));
  630. tioce_common->ce_pcibus.bs_base |= __IA64_UNCACHED_OFFSET;
  631. if (tioce_kern_init(tioce_common) == NULL) {
  632. kfree(tioce_common);
  633. return NULL;
  634. }
  635. if (request_irq(SGI_PCIASIC_ERROR,
  636. tioce_error_intr_handler,
  637. SA_SHIRQ, "TIOCE error", (void *)tioce_common))
  638. printk(KERN_WARNING
  639. "%s: Unable to get irq %d. "
  640. "Error interrupts won't be routed for "
  641. "TIOCE bus %04x:%02x\n",
  642. __FUNCTION__, SGI_PCIASIC_ERROR,
  643. tioce_common->ce_pcibus.bs_persist_segment,
  644. tioce_common->ce_pcibus.bs_persist_busnum);
  645. return tioce_common;
  646. }
  647. static struct sn_pcibus_provider tioce_pci_interfaces = {
  648. .dma_map = tioce_dma,
  649. .dma_map_consistent = tioce_dma_consistent,
  650. .dma_unmap = tioce_dma_unmap,
  651. .bus_fixup = tioce_bus_fixup,
  652. .force_interrupt = tioce_force_interrupt,
  653. .target_interrupt = tioce_target_interrupt
  654. };
  655. /**
  656. * tioce_init_provider - init SN PCI provider ops for TIO CE
  657. */
  658. int
  659. tioce_init_provider(void)
  660. {
  661. sn_pci_provider[PCIIO_ASIC_TYPE_TIOCE] = &tioce_pci_interfaces;
  662. return 0;
  663. }