tioce_provider.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  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-2006 Silicon Graphics, Inc. All Rights Reserved.
  7. */
  8. #include <linux/types.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/slab.h>
  11. #include <linux/pci.h>
  12. #include <asm/sn/sn_sal.h>
  13. #include <asm/sn/addrs.h>
  14. #include <asm/sn/io.h>
  15. #include <asm/sn/pcidev.h>
  16. #include <asm/sn/pcibus_provider_defs.h>
  17. #include <asm/sn/tioce_provider.h>
  18. /*
  19. * 1/26/2006
  20. *
  21. * WAR for SGI PV 944642. For revA TIOCE, need to use the following recipe
  22. * (taken from the above PV) before and after accessing tioce internal MMR's
  23. * to avoid tioce lockups.
  24. *
  25. * The recipe as taken from the PV:
  26. *
  27. * if(mmr address < 0x45000) {
  28. * if(mmr address == 0 or 0x80)
  29. * mmr wrt or read address 0xc0
  30. * else if(mmr address == 0x148 or 0x200)
  31. * mmr wrt or read address 0x28
  32. * else
  33. * mmr wrt or read address 0x158
  34. *
  35. * do desired mmr access (rd or wrt)
  36. *
  37. * if(mmr address == 0x100)
  38. * mmr wrt or read address 0x38
  39. * mmr wrt or read address 0xb050
  40. * } else
  41. * do desired mmr access
  42. *
  43. * According to hw, we can use reads instead of writes to the above address
  44. *
  45. * Note this WAR can only to be used for accessing internal MMR's in the
  46. * TIOCE Coretalk Address Range 0x0 - 0x07ff_ffff. This includes the
  47. * "Local CE Registers and Memories" and "PCI Compatible Config Space" address
  48. * spaces from table 2-1 of the "CE Programmer's Reference Overview" document.
  49. *
  50. * All registers defined in struct tioce will meet that criteria.
  51. */
  52. static inline void
  53. tioce_mmr_war_pre(struct tioce_kernel *kern, void __iomem *mmr_addr)
  54. {
  55. u64 mmr_base;
  56. u64 mmr_offset;
  57. if (kern->ce_common->ce_rev != TIOCE_REV_A)
  58. return;
  59. mmr_base = kern->ce_common->ce_pcibus.bs_base;
  60. mmr_offset = (unsigned long)mmr_addr - mmr_base;
  61. if (mmr_offset < 0x45000) {
  62. u64 mmr_war_offset;
  63. if (mmr_offset == 0 || mmr_offset == 0x80)
  64. mmr_war_offset = 0xc0;
  65. else if (mmr_offset == 0x148 || mmr_offset == 0x200)
  66. mmr_war_offset = 0x28;
  67. else
  68. mmr_war_offset = 0x158;
  69. readq_relaxed((void __iomem *)(mmr_base + mmr_war_offset));
  70. }
  71. }
  72. static inline void
  73. tioce_mmr_war_post(struct tioce_kernel *kern, void __iomem *mmr_addr)
  74. {
  75. u64 mmr_base;
  76. u64 mmr_offset;
  77. if (kern->ce_common->ce_rev != TIOCE_REV_A)
  78. return;
  79. mmr_base = kern->ce_common->ce_pcibus.bs_base;
  80. mmr_offset = (unsigned long)mmr_addr - mmr_base;
  81. if (mmr_offset < 0x45000) {
  82. if (mmr_offset == 0x100)
  83. readq_relaxed((void __iomem *)(mmr_base + 0x38));
  84. readq_relaxed((void __iomem *)(mmr_base + 0xb050));
  85. }
  86. }
  87. /* load mmr contents into a variable */
  88. #define tioce_mmr_load(kern, mmrp, varp) do {\
  89. tioce_mmr_war_pre(kern, mmrp); \
  90. *(varp) = readq_relaxed(mmrp); \
  91. tioce_mmr_war_post(kern, mmrp); \
  92. } while (0)
  93. /* store variable contents into mmr */
  94. #define tioce_mmr_store(kern, mmrp, varp) do {\
  95. tioce_mmr_war_pre(kern, mmrp); \
  96. writeq(*varp, mmrp); \
  97. tioce_mmr_war_post(kern, mmrp); \
  98. } while (0)
  99. /* store immediate value into mmr */
  100. #define tioce_mmr_storei(kern, mmrp, val) do {\
  101. tioce_mmr_war_pre(kern, mmrp); \
  102. writeq(val, mmrp); \
  103. tioce_mmr_war_post(kern, mmrp); \
  104. } while (0)
  105. /* set bits (immediate value) into mmr */
  106. #define tioce_mmr_seti(kern, mmrp, bits) do {\
  107. u64 tmp; \
  108. tioce_mmr_load(kern, mmrp, &tmp); \
  109. tmp |= (bits); \
  110. tioce_mmr_store(kern, mmrp, &tmp); \
  111. } while (0)
  112. /* clear bits (immediate value) into mmr */
  113. #define tioce_mmr_clri(kern, mmrp, bits) do { \
  114. u64 tmp; \
  115. tioce_mmr_load(kern, mmrp, &tmp); \
  116. tmp &= ~(bits); \
  117. tioce_mmr_store(kern, mmrp, &tmp); \
  118. } while (0)
  119. /**
  120. * Bus address ranges for the 5 flavors of TIOCE DMA
  121. */
  122. #define TIOCE_D64_MIN 0x8000000000000000UL
  123. #define TIOCE_D64_MAX 0xffffffffffffffffUL
  124. #define TIOCE_D64_ADDR(a) ((a) >= TIOCE_D64_MIN)
  125. #define TIOCE_D32_MIN 0x0000000080000000UL
  126. #define TIOCE_D32_MAX 0x00000000ffffffffUL
  127. #define TIOCE_D32_ADDR(a) ((a) >= TIOCE_D32_MIN && (a) <= TIOCE_D32_MAX)
  128. #define TIOCE_M32_MIN 0x0000000000000000UL
  129. #define TIOCE_M32_MAX 0x000000007fffffffUL
  130. #define TIOCE_M32_ADDR(a) ((a) >= TIOCE_M32_MIN && (a) <= TIOCE_M32_MAX)
  131. #define TIOCE_M40_MIN 0x0000004000000000UL
  132. #define TIOCE_M40_MAX 0x0000007fffffffffUL
  133. #define TIOCE_M40_ADDR(a) ((a) >= TIOCE_M40_MIN && (a) <= TIOCE_M40_MAX)
  134. #define TIOCE_M40S_MIN 0x0000008000000000UL
  135. #define TIOCE_M40S_MAX 0x000000ffffffffffUL
  136. #define TIOCE_M40S_ADDR(a) ((a) >= TIOCE_M40S_MIN && (a) <= TIOCE_M40S_MAX)
  137. /*
  138. * ATE manipulation macros.
  139. */
  140. #define ATE_PAGESHIFT(ps) (__ffs(ps))
  141. #define ATE_PAGEMASK(ps) ((ps)-1)
  142. #define ATE_PAGE(x, ps) ((x) >> ATE_PAGESHIFT(ps))
  143. #define ATE_NPAGES(start, len, pagesize) \
  144. (ATE_PAGE((start)+(len)-1, pagesize) - ATE_PAGE(start, pagesize) + 1)
  145. #define ATE_VALID(ate) ((ate) & (1UL << 63))
  146. #define ATE_MAKE(addr, ps, msi) \
  147. (((addr) & ~ATE_PAGEMASK(ps)) | (1UL << 63) | ((msi)?(1UL << 62):0))
  148. /*
  149. * Flavors of ate-based mapping supported by tioce_alloc_map()
  150. */
  151. #define TIOCE_ATE_M32 1
  152. #define TIOCE_ATE_M40 2
  153. #define TIOCE_ATE_M40S 3
  154. #define KB(x) ((u64)(x) << 10)
  155. #define MB(x) ((u64)(x) << 20)
  156. #define GB(x) ((u64)(x) << 30)
  157. /**
  158. * tioce_dma_d64 - create a DMA mapping using 64-bit direct mode
  159. * @ct_addr: system coretalk address
  160. *
  161. * Map @ct_addr into 64-bit CE bus space. No device context is necessary
  162. * and no CE mapping are consumed.
  163. *
  164. * Bits 53:0 come from the coretalk address. The remaining bits are set as
  165. * follows:
  166. *
  167. * 63 - must be 1 to indicate d64 mode to CE hardware
  168. * 62 - barrier bit ... controlled with tioce_dma_barrier()
  169. * 61 - msi bit ... specified through dma_flags
  170. * 60:54 - reserved, MBZ
  171. */
  172. static u64
  173. tioce_dma_d64(unsigned long ct_addr, int dma_flags)
  174. {
  175. u64 bus_addr;
  176. bus_addr = ct_addr | (1UL << 63);
  177. if (dma_flags & SN_DMA_MSI)
  178. bus_addr |= (1UL << 61);
  179. return bus_addr;
  180. }
  181. /**
  182. * pcidev_to_tioce - return misc ce related pointers given a pci_dev
  183. * @pci_dev: pci device context
  184. * @base: ptr to store struct tioce_mmr * for the CE holding this device
  185. * @kernel: ptr to store struct tioce_kernel * for the CE holding this device
  186. * @port: ptr to store the CE port number that this device is on
  187. *
  188. * Return pointers to various CE-related structures for the CE upstream of
  189. * @pci_dev.
  190. */
  191. static inline void
  192. pcidev_to_tioce(struct pci_dev *pdev, struct tioce __iomem **base,
  193. struct tioce_kernel **kernel, int *port)
  194. {
  195. struct pcidev_info *pcidev_info;
  196. struct tioce_common *ce_common;
  197. struct tioce_kernel *ce_kernel;
  198. pcidev_info = SN_PCIDEV_INFO(pdev);
  199. ce_common = (struct tioce_common *)pcidev_info->pdi_pcibus_info;
  200. ce_kernel = (struct tioce_kernel *)ce_common->ce_kernel_private;
  201. if (base)
  202. *base = (struct tioce __iomem *)ce_common->ce_pcibus.bs_base;
  203. if (kernel)
  204. *kernel = ce_kernel;
  205. /*
  206. * we use port as a zero-based value internally, even though the
  207. * documentation is 1-based.
  208. */
  209. if (port)
  210. *port =
  211. (pdev->bus->number < ce_kernel->ce_port1_secondary) ? 0 : 1;
  212. }
  213. /**
  214. * tioce_alloc_map - Given a coretalk address, map it to pcie bus address
  215. * space using one of the various ATE-based address modes.
  216. * @ce_kern: tioce context
  217. * @type: map mode to use
  218. * @port: 0-based port that the requesting device is downstream of
  219. * @ct_addr: the coretalk address to map
  220. * @len: number of bytes to map
  221. *
  222. * Given the addressing type, set up various parameters that define the
  223. * ATE pool to use. Search for a contiguous block of entries to cover the
  224. * length, and if enough resources exist, fill in the ATEs and construct a
  225. * tioce_dmamap struct to track the mapping.
  226. */
  227. static u64
  228. tioce_alloc_map(struct tioce_kernel *ce_kern, int type, int port,
  229. u64 ct_addr, int len, int dma_flags)
  230. {
  231. int i;
  232. int j;
  233. int first;
  234. int last;
  235. int entries;
  236. int nates;
  237. u64 pagesize;
  238. int msi_capable, msi_wanted;
  239. u64 *ate_shadow;
  240. u64 __iomem *ate_reg;
  241. u64 addr;
  242. struct tioce __iomem *ce_mmr;
  243. u64 bus_base;
  244. struct tioce_dmamap *map;
  245. ce_mmr = (struct tioce __iomem *)ce_kern->ce_common->ce_pcibus.bs_base;
  246. switch (type) {
  247. case TIOCE_ATE_M32:
  248. /*
  249. * The first 64 entries of the ate3240 pool are dedicated to
  250. * super-page (TIOCE_ATE_M40S) mode.
  251. */
  252. first = 64;
  253. entries = TIOCE_NUM_M3240_ATES - 64;
  254. ate_shadow = ce_kern->ce_ate3240_shadow;
  255. ate_reg = ce_mmr->ce_ure_ate3240;
  256. pagesize = ce_kern->ce_ate3240_pagesize;
  257. bus_base = TIOCE_M32_MIN;
  258. msi_capable = 1;
  259. break;
  260. case TIOCE_ATE_M40:
  261. first = 0;
  262. entries = TIOCE_NUM_M40_ATES;
  263. ate_shadow = ce_kern->ce_ate40_shadow;
  264. ate_reg = ce_mmr->ce_ure_ate40;
  265. pagesize = MB(64);
  266. bus_base = TIOCE_M40_MIN;
  267. msi_capable = 0;
  268. break;
  269. case TIOCE_ATE_M40S:
  270. /*
  271. * ate3240 entries 0-31 are dedicated to port1 super-page
  272. * mappings. ate3240 entries 32-63 are dedicated to port2.
  273. */
  274. first = port * 32;
  275. entries = 32;
  276. ate_shadow = ce_kern->ce_ate3240_shadow;
  277. ate_reg = ce_mmr->ce_ure_ate3240;
  278. pagesize = GB(16);
  279. bus_base = TIOCE_M40S_MIN;
  280. msi_capable = 0;
  281. break;
  282. default:
  283. return 0;
  284. }
  285. msi_wanted = dma_flags & SN_DMA_MSI;
  286. if (msi_wanted && !msi_capable)
  287. return 0;
  288. nates = ATE_NPAGES(ct_addr, len, pagesize);
  289. if (nates > entries)
  290. return 0;
  291. last = first + entries - nates;
  292. for (i = first; i <= last; i++) {
  293. if (ATE_VALID(ate_shadow[i]))
  294. continue;
  295. for (j = i; j < i + nates; j++)
  296. if (ATE_VALID(ate_shadow[j]))
  297. break;
  298. if (j >= i + nates)
  299. break;
  300. }
  301. if (i > last)
  302. return 0;
  303. map = kzalloc(sizeof(struct tioce_dmamap), GFP_ATOMIC);
  304. if (!map)
  305. return 0;
  306. addr = ct_addr;
  307. for (j = 0; j < nates; j++) {
  308. u64 ate;
  309. ate = ATE_MAKE(addr, pagesize, msi_wanted);
  310. ate_shadow[i + j] = ate;
  311. tioce_mmr_storei(ce_kern, &ate_reg[i + j], ate);
  312. addr += pagesize;
  313. }
  314. map->refcnt = 1;
  315. map->nbytes = nates * pagesize;
  316. map->ct_start = ct_addr & ~ATE_PAGEMASK(pagesize);
  317. map->pci_start = bus_base + (i * pagesize);
  318. map->ate_hw = &ate_reg[i];
  319. map->ate_shadow = &ate_shadow[i];
  320. map->ate_count = nates;
  321. list_add(&map->ce_dmamap_list, &ce_kern->ce_dmamap_list);
  322. return (map->pci_start + (ct_addr - map->ct_start));
  323. }
  324. /**
  325. * tioce_dma_d32 - create a DMA mapping using 32-bit direct mode
  326. * @pdev: linux pci_dev representing the function
  327. * @paddr: system physical address
  328. *
  329. * Map @paddr into 32-bit bus space of the CE associated with @pcidev_info.
  330. */
  331. static u64
  332. tioce_dma_d32(struct pci_dev *pdev, u64 ct_addr, int dma_flags)
  333. {
  334. int dma_ok;
  335. int port;
  336. struct tioce __iomem *ce_mmr;
  337. struct tioce_kernel *ce_kern;
  338. u64 ct_upper;
  339. u64 ct_lower;
  340. dma_addr_t bus_addr;
  341. if (dma_flags & SN_DMA_MSI)
  342. return 0;
  343. ct_upper = ct_addr & ~0x3fffffffUL;
  344. ct_lower = ct_addr & 0x3fffffffUL;
  345. pcidev_to_tioce(pdev, &ce_mmr, &ce_kern, &port);
  346. if (ce_kern->ce_port[port].dirmap_refcnt == 0) {
  347. u64 tmp;
  348. ce_kern->ce_port[port].dirmap_shadow = ct_upper;
  349. tioce_mmr_storei(ce_kern, &ce_mmr->ce_ure_dir_map[port],
  350. ct_upper);
  351. tmp = ce_mmr->ce_ure_dir_map[port];
  352. dma_ok = 1;
  353. } else
  354. dma_ok = (ce_kern->ce_port[port].dirmap_shadow == ct_upper);
  355. if (dma_ok) {
  356. ce_kern->ce_port[port].dirmap_refcnt++;
  357. bus_addr = TIOCE_D32_MIN + ct_lower;
  358. } else
  359. bus_addr = 0;
  360. return bus_addr;
  361. }
  362. /**
  363. * tioce_dma_barrier - swizzle a TIOCE bus address to include or exclude
  364. * the barrier bit.
  365. * @bus_addr: bus address to swizzle
  366. *
  367. * Given a TIOCE bus address, set the appropriate bit to indicate barrier
  368. * attributes.
  369. */
  370. static u64
  371. tioce_dma_barrier(u64 bus_addr, int on)
  372. {
  373. u64 barrier_bit;
  374. /* barrier not supported in M40/M40S mode */
  375. if (TIOCE_M40_ADDR(bus_addr) || TIOCE_M40S_ADDR(bus_addr))
  376. return bus_addr;
  377. if (TIOCE_D64_ADDR(bus_addr))
  378. barrier_bit = (1UL << 62);
  379. else /* must be m32 or d32 */
  380. barrier_bit = (1UL << 30);
  381. return (on) ? (bus_addr | barrier_bit) : (bus_addr & ~barrier_bit);
  382. }
  383. /**
  384. * tioce_dma_unmap - release CE mapping resources
  385. * @pdev: linux pci_dev representing the function
  386. * @bus_addr: bus address returned by an earlier tioce_dma_map
  387. * @dir: mapping direction (unused)
  388. *
  389. * Locate mapping resources associated with @bus_addr and release them.
  390. * For mappings created using the direct modes there are no resources
  391. * to release.
  392. */
  393. void
  394. tioce_dma_unmap(struct pci_dev *pdev, dma_addr_t bus_addr, int dir)
  395. {
  396. int i;
  397. int port;
  398. struct tioce_kernel *ce_kern;
  399. struct tioce __iomem *ce_mmr;
  400. unsigned long flags;
  401. bus_addr = tioce_dma_barrier(bus_addr, 0);
  402. pcidev_to_tioce(pdev, &ce_mmr, &ce_kern, &port);
  403. /* nothing to do for D64 */
  404. if (TIOCE_D64_ADDR(bus_addr))
  405. return;
  406. spin_lock_irqsave(&ce_kern->ce_lock, flags);
  407. if (TIOCE_D32_ADDR(bus_addr)) {
  408. if (--ce_kern->ce_port[port].dirmap_refcnt == 0) {
  409. ce_kern->ce_port[port].dirmap_shadow = 0;
  410. tioce_mmr_storei(ce_kern, &ce_mmr->ce_ure_dir_map[port],
  411. 0);
  412. }
  413. } else {
  414. struct tioce_dmamap *map;
  415. list_for_each_entry(map, &ce_kern->ce_dmamap_list,
  416. ce_dmamap_list) {
  417. u64 last;
  418. last = map->pci_start + map->nbytes - 1;
  419. if (bus_addr >= map->pci_start && bus_addr <= last)
  420. break;
  421. }
  422. if (&map->ce_dmamap_list == &ce_kern->ce_dmamap_list) {
  423. printk(KERN_WARNING
  424. "%s: %s - no map found for bus_addr 0x%llx\n",
  425. __func__, pci_name(pdev), bus_addr);
  426. } else if (--map->refcnt == 0) {
  427. for (i = 0; i < map->ate_count; i++) {
  428. map->ate_shadow[i] = 0;
  429. tioce_mmr_storei(ce_kern, &map->ate_hw[i], 0);
  430. }
  431. list_del(&map->ce_dmamap_list);
  432. kfree(map);
  433. }
  434. }
  435. spin_unlock_irqrestore(&ce_kern->ce_lock, flags);
  436. }
  437. /**
  438. * tioce_do_dma_map - map pages for PCI DMA
  439. * @pdev: linux pci_dev representing the function
  440. * @paddr: host physical address to map
  441. * @byte_count: bytes to map
  442. *
  443. * This is the main wrapper for mapping host physical pages to CE PCI space.
  444. * The mapping mode used is based on the device's dma_mask.
  445. */
  446. static u64
  447. tioce_do_dma_map(struct pci_dev *pdev, u64 paddr, size_t byte_count,
  448. int barrier, int dma_flags)
  449. {
  450. unsigned long flags;
  451. u64 ct_addr;
  452. u64 mapaddr = 0;
  453. struct tioce_kernel *ce_kern;
  454. struct tioce_dmamap *map;
  455. int port;
  456. u64 dma_mask;
  457. dma_mask = (barrier) ? pdev->dev.coherent_dma_mask : pdev->dma_mask;
  458. /* cards must be able to address at least 31 bits */
  459. if (dma_mask < 0x7fffffffUL)
  460. return 0;
  461. if (SN_DMA_ADDRTYPE(dma_flags) == SN_DMA_ADDR_PHYS)
  462. ct_addr = PHYS_TO_TIODMA(paddr);
  463. else
  464. ct_addr = paddr;
  465. /*
  466. * If the device can generate 64 bit addresses, create a D64 map.
  467. */
  468. if (dma_mask == ~0UL) {
  469. mapaddr = tioce_dma_d64(ct_addr, dma_flags);
  470. if (mapaddr)
  471. goto dma_map_done;
  472. }
  473. pcidev_to_tioce(pdev, NULL, &ce_kern, &port);
  474. spin_lock_irqsave(&ce_kern->ce_lock, flags);
  475. /*
  476. * D64 didn't work ... See if we have an existing map that covers
  477. * this address range. Must account for devices dma_mask here since
  478. * an existing map might have been done in a mode using more pci
  479. * address bits than this device can support.
  480. */
  481. list_for_each_entry(map, &ce_kern->ce_dmamap_list, ce_dmamap_list) {
  482. u64 last;
  483. last = map->ct_start + map->nbytes - 1;
  484. if (ct_addr >= map->ct_start &&
  485. ct_addr + byte_count - 1 <= last &&
  486. map->pci_start <= dma_mask) {
  487. map->refcnt++;
  488. mapaddr = map->pci_start + (ct_addr - map->ct_start);
  489. break;
  490. }
  491. }
  492. /*
  493. * If we don't have a map yet, and the card can generate 40
  494. * bit addresses, try the M40/M40S modes. Note these modes do not
  495. * support a barrier bit, so if we need a consistent map these
  496. * won't work.
  497. */
  498. if (!mapaddr && !barrier && dma_mask >= 0xffffffffffUL) {
  499. /*
  500. * We have two options for 40-bit mappings: 16GB "super" ATEs
  501. * and 64MB "regular" ATEs. We'll try both if needed for a
  502. * given mapping but which one we try first depends on the
  503. * size. For requests >64MB, prefer to use a super page with
  504. * regular as the fallback. Otherwise, try in the reverse order.
  505. */
  506. if (byte_count > MB(64)) {
  507. mapaddr = tioce_alloc_map(ce_kern, TIOCE_ATE_M40S,
  508. port, ct_addr, byte_count,
  509. dma_flags);
  510. if (!mapaddr)
  511. mapaddr =
  512. tioce_alloc_map(ce_kern, TIOCE_ATE_M40, -1,
  513. ct_addr, byte_count,
  514. dma_flags);
  515. } else {
  516. mapaddr = tioce_alloc_map(ce_kern, TIOCE_ATE_M40, -1,
  517. ct_addr, byte_count,
  518. dma_flags);
  519. if (!mapaddr)
  520. mapaddr =
  521. tioce_alloc_map(ce_kern, TIOCE_ATE_M40S,
  522. port, ct_addr, byte_count,
  523. dma_flags);
  524. }
  525. }
  526. /*
  527. * 32-bit direct is the next mode to try
  528. */
  529. if (!mapaddr && dma_mask >= 0xffffffffUL)
  530. mapaddr = tioce_dma_d32(pdev, ct_addr, dma_flags);
  531. /*
  532. * Last resort, try 32-bit ATE-based map.
  533. */
  534. if (!mapaddr)
  535. mapaddr =
  536. tioce_alloc_map(ce_kern, TIOCE_ATE_M32, -1, ct_addr,
  537. byte_count, dma_flags);
  538. spin_unlock_irqrestore(&ce_kern->ce_lock, flags);
  539. dma_map_done:
  540. if (mapaddr && barrier)
  541. mapaddr = tioce_dma_barrier(mapaddr, 1);
  542. return mapaddr;
  543. }
  544. /**
  545. * tioce_dma - standard pci dma map interface
  546. * @pdev: pci device requesting the map
  547. * @paddr: system physical address to map into pci space
  548. * @byte_count: # bytes to map
  549. *
  550. * Simply call tioce_do_dma_map() to create a map with the barrier bit clear
  551. * in the address.
  552. */
  553. static u64
  554. tioce_dma(struct pci_dev *pdev, unsigned long paddr, size_t byte_count, int dma_flags)
  555. {
  556. return tioce_do_dma_map(pdev, paddr, byte_count, 0, dma_flags);
  557. }
  558. /**
  559. * tioce_dma_consistent - consistent pci dma map interface
  560. * @pdev: pci device requesting the map
  561. * @paddr: system physical address to map into pci space
  562. * @byte_count: # bytes to map
  563. *
  564. * Simply call tioce_do_dma_map() to create a map with the barrier bit set
  565. * in the address.
  566. */
  567. static u64
  568. tioce_dma_consistent(struct pci_dev *pdev, unsigned long paddr, size_t byte_count, int dma_flags)
  569. {
  570. return tioce_do_dma_map(pdev, paddr, byte_count, 1, dma_flags);
  571. }
  572. /**
  573. * tioce_error_intr_handler - SGI TIO CE error interrupt handler
  574. * @irq: unused
  575. * @arg: pointer to tioce_common struct for the given CE
  576. *
  577. * Handle a CE error interrupt. Simply a wrapper around a SAL call which
  578. * defers processing to the SGI prom.
  579. */
  580. static irqreturn_t
  581. tioce_error_intr_handler(int irq, void *arg)
  582. {
  583. struct tioce_common *soft = arg;
  584. struct ia64_sal_retval ret_stuff;
  585. ret_stuff.status = 0;
  586. ret_stuff.v0 = 0;
  587. SAL_CALL_NOLOCK(ret_stuff, (u64) SN_SAL_IOIF_ERROR_INTERRUPT,
  588. soft->ce_pcibus.bs_persist_segment,
  589. soft->ce_pcibus.bs_persist_busnum, 0, 0, 0, 0, 0);
  590. if (ret_stuff.v0)
  591. panic("tioce_error_intr_handler: Fatal TIOCE error");
  592. return IRQ_HANDLED;
  593. }
  594. /**
  595. * tioce_reserve_m32 - reserve M32 ATEs for the indicated address range
  596. * @tioce_kernel: TIOCE context to reserve ATEs for
  597. * @base: starting bus address to reserve
  598. * @limit: last bus address to reserve
  599. *
  600. * If base/limit falls within the range of bus space mapped through the
  601. * M32 space, reserve the resources corresponding to the range.
  602. */
  603. static void
  604. tioce_reserve_m32(struct tioce_kernel *ce_kern, u64 base, u64 limit)
  605. {
  606. int ate_index, last_ate, ps;
  607. struct tioce __iomem *ce_mmr;
  608. ce_mmr = (struct tioce __iomem *)ce_kern->ce_common->ce_pcibus.bs_base;
  609. ps = ce_kern->ce_ate3240_pagesize;
  610. ate_index = ATE_PAGE(base, ps);
  611. last_ate = ate_index + ATE_NPAGES(base, limit-base+1, ps) - 1;
  612. if (ate_index < 64)
  613. ate_index = 64;
  614. if (last_ate >= TIOCE_NUM_M3240_ATES)
  615. last_ate = TIOCE_NUM_M3240_ATES - 1;
  616. while (ate_index <= last_ate) {
  617. u64 ate;
  618. ate = ATE_MAKE(0xdeadbeef, ps, 0);
  619. ce_kern->ce_ate3240_shadow[ate_index] = ate;
  620. tioce_mmr_storei(ce_kern, &ce_mmr->ce_ure_ate3240[ate_index],
  621. ate);
  622. ate_index++;
  623. }
  624. }
  625. /**
  626. * tioce_kern_init - init kernel structures related to a given TIOCE
  627. * @tioce_common: ptr to a cached tioce_common struct that originated in prom
  628. */
  629. static struct tioce_kernel *
  630. tioce_kern_init(struct tioce_common *tioce_common)
  631. {
  632. int i;
  633. int ps;
  634. int dev;
  635. u32 tmp;
  636. unsigned int seg, bus;
  637. struct tioce __iomem *tioce_mmr;
  638. struct tioce_kernel *tioce_kern;
  639. tioce_kern = kzalloc(sizeof(struct tioce_kernel), GFP_KERNEL);
  640. if (!tioce_kern) {
  641. return NULL;
  642. }
  643. tioce_kern->ce_common = tioce_common;
  644. spin_lock_init(&tioce_kern->ce_lock);
  645. INIT_LIST_HEAD(&tioce_kern->ce_dmamap_list);
  646. tioce_common->ce_kernel_private = (u64) tioce_kern;
  647. /*
  648. * Determine the secondary bus number of the port2 logical PPB.
  649. * This is used to decide whether a given pci device resides on
  650. * port1 or port2. Note: We don't have enough plumbing set up
  651. * here to use pci_read_config_xxx() so use raw_pci_read().
  652. */
  653. seg = tioce_common->ce_pcibus.bs_persist_segment;
  654. bus = tioce_common->ce_pcibus.bs_persist_busnum;
  655. raw_pci_read(seg, bus, PCI_DEVFN(2, 0), PCI_SECONDARY_BUS, 1,&tmp);
  656. tioce_kern->ce_port1_secondary = (u8) tmp;
  657. /*
  658. * Set PMU pagesize to the largest size available, and zero out
  659. * the ATEs.
  660. */
  661. tioce_mmr = (struct tioce __iomem *)tioce_common->ce_pcibus.bs_base;
  662. tioce_mmr_clri(tioce_kern, &tioce_mmr->ce_ure_page_map,
  663. CE_URE_PAGESIZE_MASK);
  664. tioce_mmr_seti(tioce_kern, &tioce_mmr->ce_ure_page_map,
  665. CE_URE_256K_PAGESIZE);
  666. ps = tioce_kern->ce_ate3240_pagesize = KB(256);
  667. for (i = 0; i < TIOCE_NUM_M40_ATES; i++) {
  668. tioce_kern->ce_ate40_shadow[i] = 0;
  669. tioce_mmr_storei(tioce_kern, &tioce_mmr->ce_ure_ate40[i], 0);
  670. }
  671. for (i = 0; i < TIOCE_NUM_M3240_ATES; i++) {
  672. tioce_kern->ce_ate3240_shadow[i] = 0;
  673. tioce_mmr_storei(tioce_kern, &tioce_mmr->ce_ure_ate3240[i], 0);
  674. }
  675. /*
  676. * Reserve ATEs corresponding to reserved address ranges. These
  677. * include:
  678. *
  679. * Memory space covered by each PPB mem base/limit register
  680. * Memory space covered by each PPB prefetch base/limit register
  681. *
  682. * These bus ranges are for pio (downstream) traffic only, and so
  683. * cannot be used for DMA.
  684. */
  685. for (dev = 1; dev <= 2; dev++) {
  686. u64 base, limit;
  687. /* mem base/limit */
  688. raw_pci_read(seg, bus, PCI_DEVFN(dev, 0),
  689. PCI_MEMORY_BASE, 2, &tmp);
  690. base = (u64)tmp << 16;
  691. raw_pci_read(seg, bus, PCI_DEVFN(dev, 0),
  692. PCI_MEMORY_LIMIT, 2, &tmp);
  693. limit = (u64)tmp << 16;
  694. limit |= 0xfffffUL;
  695. if (base < limit)
  696. tioce_reserve_m32(tioce_kern, base, limit);
  697. /*
  698. * prefetch mem base/limit. The tioce ppb's have 64-bit
  699. * decoders, so read the upper portions w/o checking the
  700. * attributes.
  701. */
  702. raw_pci_read(seg, bus, PCI_DEVFN(dev, 0),
  703. PCI_PREF_MEMORY_BASE, 2, &tmp);
  704. base = ((u64)tmp & PCI_PREF_RANGE_MASK) << 16;
  705. raw_pci_read(seg, bus, PCI_DEVFN(dev, 0),
  706. PCI_PREF_BASE_UPPER32, 4, &tmp);
  707. base |= (u64)tmp << 32;
  708. raw_pci_read(seg, bus, PCI_DEVFN(dev, 0),
  709. PCI_PREF_MEMORY_LIMIT, 2, &tmp);
  710. limit = ((u64)tmp & PCI_PREF_RANGE_MASK) << 16;
  711. limit |= 0xfffffUL;
  712. raw_pci_read(seg, bus, PCI_DEVFN(dev, 0),
  713. PCI_PREF_LIMIT_UPPER32, 4, &tmp);
  714. limit |= (u64)tmp << 32;
  715. if ((base < limit) && TIOCE_M32_ADDR(base))
  716. tioce_reserve_m32(tioce_kern, base, limit);
  717. }
  718. return tioce_kern;
  719. }
  720. /**
  721. * tioce_force_interrupt - implement altix force_interrupt() backend for CE
  722. * @sn_irq_info: sn asic irq that we need an interrupt generated for
  723. *
  724. * Given an sn_irq_info struct, set the proper bit in ce_adm_force_int to
  725. * force a secondary interrupt to be generated. This is to work around an
  726. * asic issue where there is a small window of opportunity for a legacy device
  727. * interrupt to be lost.
  728. */
  729. static void
  730. tioce_force_interrupt(struct sn_irq_info *sn_irq_info)
  731. {
  732. struct pcidev_info *pcidev_info;
  733. struct tioce_common *ce_common;
  734. struct tioce_kernel *ce_kern;
  735. struct tioce __iomem *ce_mmr;
  736. u64 force_int_val;
  737. if (!sn_irq_info->irq_bridge)
  738. return;
  739. if (sn_irq_info->irq_bridge_type != PCIIO_ASIC_TYPE_TIOCE)
  740. return;
  741. pcidev_info = (struct pcidev_info *)sn_irq_info->irq_pciioinfo;
  742. if (!pcidev_info)
  743. return;
  744. ce_common = (struct tioce_common *)pcidev_info->pdi_pcibus_info;
  745. ce_mmr = (struct tioce __iomem *)ce_common->ce_pcibus.bs_base;
  746. ce_kern = (struct tioce_kernel *)ce_common->ce_kernel_private;
  747. /*
  748. * TIOCE Rev A workaround (PV 945826), force an interrupt by writing
  749. * the TIO_INTx register directly (1/26/2006)
  750. */
  751. if (ce_common->ce_rev == TIOCE_REV_A) {
  752. u64 int_bit_mask = (1ULL << sn_irq_info->irq_int_bit);
  753. u64 status;
  754. tioce_mmr_load(ce_kern, &ce_mmr->ce_adm_int_status, &status);
  755. if (status & int_bit_mask) {
  756. u64 force_irq = (1 << 8) | sn_irq_info->irq_irq;
  757. u64 ctalk = sn_irq_info->irq_xtalkaddr;
  758. u64 nasid, offset;
  759. nasid = (ctalk & CTALK_NASID_MASK) >> CTALK_NASID_SHFT;
  760. offset = (ctalk & CTALK_NODE_OFFSET);
  761. HUB_S(TIO_IOSPACE_ADDR(nasid, offset), force_irq);
  762. }
  763. return;
  764. }
  765. /*
  766. * irq_int_bit is originally set up by prom, and holds the interrupt
  767. * bit shift (not mask) as defined by the bit definitions in the
  768. * ce_adm_int mmr. These shifts are not the same for the
  769. * ce_adm_force_int register, so do an explicit mapping here to make
  770. * things clearer.
  771. */
  772. switch (sn_irq_info->irq_int_bit) {
  773. case CE_ADM_INT_PCIE_PORT1_DEV_A_SHFT:
  774. force_int_val = 1UL << CE_ADM_FORCE_INT_PCIE_PORT1_DEV_A_SHFT;
  775. break;
  776. case CE_ADM_INT_PCIE_PORT1_DEV_B_SHFT:
  777. force_int_val = 1UL << CE_ADM_FORCE_INT_PCIE_PORT1_DEV_B_SHFT;
  778. break;
  779. case CE_ADM_INT_PCIE_PORT1_DEV_C_SHFT:
  780. force_int_val = 1UL << CE_ADM_FORCE_INT_PCIE_PORT1_DEV_C_SHFT;
  781. break;
  782. case CE_ADM_INT_PCIE_PORT1_DEV_D_SHFT:
  783. force_int_val = 1UL << CE_ADM_FORCE_INT_PCIE_PORT1_DEV_D_SHFT;
  784. break;
  785. case CE_ADM_INT_PCIE_PORT2_DEV_A_SHFT:
  786. force_int_val = 1UL << CE_ADM_FORCE_INT_PCIE_PORT2_DEV_A_SHFT;
  787. break;
  788. case CE_ADM_INT_PCIE_PORT2_DEV_B_SHFT:
  789. force_int_val = 1UL << CE_ADM_FORCE_INT_PCIE_PORT2_DEV_B_SHFT;
  790. break;
  791. case CE_ADM_INT_PCIE_PORT2_DEV_C_SHFT:
  792. force_int_val = 1UL << CE_ADM_FORCE_INT_PCIE_PORT2_DEV_C_SHFT;
  793. break;
  794. case CE_ADM_INT_PCIE_PORT2_DEV_D_SHFT:
  795. force_int_val = 1UL << CE_ADM_FORCE_INT_PCIE_PORT2_DEV_D_SHFT;
  796. break;
  797. default:
  798. return;
  799. }
  800. tioce_mmr_storei(ce_kern, &ce_mmr->ce_adm_force_int, force_int_val);
  801. }
  802. /**
  803. * tioce_target_interrupt - implement set_irq_affinity for tioce resident
  804. * functions. Note: only applies to line interrupts, not MSI's.
  805. *
  806. * @sn_irq_info: SN IRQ context
  807. *
  808. * Given an sn_irq_info, set the associated CE device's interrupt destination
  809. * register. Since the interrupt destination registers are on a per-ce-slot
  810. * basis, this will retarget line interrupts for all functions downstream of
  811. * the slot.
  812. */
  813. static void
  814. tioce_target_interrupt(struct sn_irq_info *sn_irq_info)
  815. {
  816. struct pcidev_info *pcidev_info;
  817. struct tioce_common *ce_common;
  818. struct tioce_kernel *ce_kern;
  819. struct tioce __iomem *ce_mmr;
  820. int bit;
  821. u64 vector;
  822. pcidev_info = (struct pcidev_info *)sn_irq_info->irq_pciioinfo;
  823. if (!pcidev_info)
  824. return;
  825. ce_common = (struct tioce_common *)pcidev_info->pdi_pcibus_info;
  826. ce_mmr = (struct tioce __iomem *)ce_common->ce_pcibus.bs_base;
  827. ce_kern = (struct tioce_kernel *)ce_common->ce_kernel_private;
  828. bit = sn_irq_info->irq_int_bit;
  829. tioce_mmr_seti(ce_kern, &ce_mmr->ce_adm_int_mask, (1UL << bit));
  830. vector = (u64)sn_irq_info->irq_irq << INTR_VECTOR_SHFT;
  831. vector |= sn_irq_info->irq_xtalkaddr;
  832. tioce_mmr_storei(ce_kern, &ce_mmr->ce_adm_int_dest[bit], vector);
  833. tioce_mmr_clri(ce_kern, &ce_mmr->ce_adm_int_mask, (1UL << bit));
  834. tioce_force_interrupt(sn_irq_info);
  835. }
  836. /**
  837. * tioce_bus_fixup - perform final PCI fixup for a TIO CE bus
  838. * @prom_bussoft: Common prom/kernel struct representing the bus
  839. *
  840. * Replicates the tioce_common pointed to by @prom_bussoft in kernel
  841. * space. Allocates and initializes a kernel-only area for a given CE,
  842. * and sets up an irq for handling CE error interrupts.
  843. *
  844. * On successful setup, returns the kernel version of tioce_common back to
  845. * the caller.
  846. */
  847. static void *
  848. tioce_bus_fixup(struct pcibus_bussoft *prom_bussoft, struct pci_controller *controller)
  849. {
  850. struct tioce_common *tioce_common;
  851. struct tioce_kernel *tioce_kern;
  852. struct tioce __iomem *tioce_mmr;
  853. /*
  854. * Allocate kernel bus soft and copy from prom.
  855. */
  856. tioce_common = kzalloc(sizeof(struct tioce_common), GFP_KERNEL);
  857. if (!tioce_common)
  858. return NULL;
  859. memcpy(tioce_common, prom_bussoft, sizeof(struct tioce_common));
  860. tioce_common->ce_pcibus.bs_base = (unsigned long)
  861. ioremap(REGION_OFFSET(tioce_common->ce_pcibus.bs_base),
  862. sizeof(struct tioce_common));
  863. tioce_kern = tioce_kern_init(tioce_common);
  864. if (tioce_kern == NULL) {
  865. kfree(tioce_common);
  866. return NULL;
  867. }
  868. /*
  869. * Clear out any transient errors before registering the error
  870. * interrupt handler.
  871. */
  872. tioce_mmr = (struct tioce __iomem *)tioce_common->ce_pcibus.bs_base;
  873. tioce_mmr_seti(tioce_kern, &tioce_mmr->ce_adm_int_status_alias, ~0ULL);
  874. tioce_mmr_seti(tioce_kern, &tioce_mmr->ce_adm_error_summary_alias,
  875. ~0ULL);
  876. tioce_mmr_seti(tioce_kern, &tioce_mmr->ce_dre_comp_err_addr, 0ULL);
  877. if (request_irq(SGI_PCIASIC_ERROR,
  878. tioce_error_intr_handler,
  879. IRQF_SHARED, "TIOCE error", (void *)tioce_common))
  880. printk(KERN_WARNING
  881. "%s: Unable to get irq %d. "
  882. "Error interrupts won't be routed for "
  883. "TIOCE bus %04x:%02x\n",
  884. __func__, SGI_PCIASIC_ERROR,
  885. tioce_common->ce_pcibus.bs_persist_segment,
  886. tioce_common->ce_pcibus.bs_persist_busnum);
  887. irq_set_handler(SGI_PCIASIC_ERROR, handle_level_irq);
  888. sn_set_err_irq_affinity(SGI_PCIASIC_ERROR);
  889. return tioce_common;
  890. }
  891. static struct sn_pcibus_provider tioce_pci_interfaces = {
  892. .dma_map = tioce_dma,
  893. .dma_map_consistent = tioce_dma_consistent,
  894. .dma_unmap = tioce_dma_unmap,
  895. .bus_fixup = tioce_bus_fixup,
  896. .force_interrupt = tioce_force_interrupt,
  897. .target_interrupt = tioce_target_interrupt
  898. };
  899. /**
  900. * tioce_init_provider - init SN PCI provider ops for TIO CE
  901. */
  902. int
  903. tioce_init_provider(void)
  904. {
  905. sn_pci_provider[PCIIO_ASIC_TYPE_TIOCE] = &tioce_pci_interfaces;
  906. return 0;
  907. }