pci.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2016-2019 HabanaLabs, Ltd.
  4. * All Rights Reserved.
  5. */
  6. #include "../habanalabs.h"
  7. #include "../../include/hw_ip/pci/pci_general.h"
  8. #include <linux/pci.h>
  9. #define HL_PLDM_PCI_ELBI_TIMEOUT_MSEC (HL_PCI_ELBI_TIMEOUT_MSEC * 100)
  10. #define IATU_REGION_CTRL_REGION_EN_MASK BIT(31)
  11. #define IATU_REGION_CTRL_MATCH_MODE_MASK BIT(30)
  12. #define IATU_REGION_CTRL_NUM_MATCH_EN_MASK BIT(19)
  13. #define IATU_REGION_CTRL_BAR_NUM_MASK GENMASK(10, 8)
  14. /**
  15. * hl_pci_bars_map() - Map PCI BARs.
  16. * @hdev: Pointer to hl_device structure.
  17. * @name: Array of BAR names.
  18. * @is_wc: Array with flag per BAR whether a write-combined mapping is needed.
  19. *
  20. * Request PCI regions and map them to kernel virtual addresses.
  21. *
  22. * Return: 0 on success, non-zero for failure.
  23. */
  24. int hl_pci_bars_map(struct hl_device *hdev, const char * const name[3],
  25. bool is_wc[3])
  26. {
  27. struct pci_dev *pdev = hdev->pdev;
  28. int rc, i, bar;
  29. rc = pci_request_regions(pdev, HL_NAME);
  30. if (rc) {
  31. dev_err(hdev->dev, "Cannot obtain PCI resources\n");
  32. return rc;
  33. }
  34. for (i = 0 ; i < 3 ; i++) {
  35. bar = i * 2; /* 64-bit BARs */
  36. hdev->pcie_bar[bar] = is_wc[i] ?
  37. pci_ioremap_wc_bar(pdev, bar) :
  38. pci_ioremap_bar(pdev, bar);
  39. if (!hdev->pcie_bar[bar]) {
  40. dev_err(hdev->dev, "pci_ioremap%s_bar failed for %s\n",
  41. is_wc[i] ? "_wc" : "", name[i]);
  42. rc = -ENODEV;
  43. goto err;
  44. }
  45. }
  46. return 0;
  47. err:
  48. for (i = 2 ; i >= 0 ; i--) {
  49. bar = i * 2; /* 64-bit BARs */
  50. if (hdev->pcie_bar[bar])
  51. iounmap(hdev->pcie_bar[bar]);
  52. }
  53. pci_release_regions(pdev);
  54. return rc;
  55. }
  56. /**
  57. * hl_pci_bars_unmap() - Unmap PCI BARS.
  58. * @hdev: Pointer to hl_device structure.
  59. *
  60. * Release all PCI BARs and unmap their virtual addresses.
  61. */
  62. static void hl_pci_bars_unmap(struct hl_device *hdev)
  63. {
  64. struct pci_dev *pdev = hdev->pdev;
  65. int i, bar;
  66. for (i = 2 ; i >= 0 ; i--) {
  67. bar = i * 2; /* 64-bit BARs */
  68. iounmap(hdev->pcie_bar[bar]);
  69. }
  70. pci_release_regions(pdev);
  71. }
  72. int hl_pci_elbi_read(struct hl_device *hdev, u64 addr, u32 *data)
  73. {
  74. struct pci_dev *pdev = hdev->pdev;
  75. ktime_t timeout;
  76. u64 msec;
  77. u32 val;
  78. if (hdev->pldm)
  79. msec = HL_PLDM_PCI_ELBI_TIMEOUT_MSEC;
  80. else
  81. msec = HL_PCI_ELBI_TIMEOUT_MSEC;
  82. /* Clear previous status */
  83. pci_write_config_dword(pdev, mmPCI_CONFIG_ELBI_STS, 0);
  84. pci_write_config_dword(pdev, mmPCI_CONFIG_ELBI_ADDR, (u32) addr);
  85. pci_write_config_dword(pdev, mmPCI_CONFIG_ELBI_CTRL, 0);
  86. timeout = ktime_add_ms(ktime_get(), msec);
  87. for (;;) {
  88. pci_read_config_dword(pdev, mmPCI_CONFIG_ELBI_STS, &val);
  89. if (val & PCI_CONFIG_ELBI_STS_MASK)
  90. break;
  91. if (ktime_compare(ktime_get(), timeout) > 0) {
  92. pci_read_config_dword(pdev, mmPCI_CONFIG_ELBI_STS,
  93. &val);
  94. break;
  95. }
  96. usleep_range(300, 500);
  97. }
  98. if ((val & PCI_CONFIG_ELBI_STS_MASK) == PCI_CONFIG_ELBI_STS_DONE) {
  99. pci_read_config_dword(pdev, mmPCI_CONFIG_ELBI_DATA, data);
  100. return 0;
  101. }
  102. if (val & PCI_CONFIG_ELBI_STS_ERR) {
  103. dev_err(hdev->dev, "Error reading from ELBI\n");
  104. return -EIO;
  105. }
  106. if (!(val & PCI_CONFIG_ELBI_STS_MASK)) {
  107. dev_err(hdev->dev, "ELBI read didn't finish in time\n");
  108. return -EIO;
  109. }
  110. dev_err(hdev->dev, "ELBI read has undefined bits in status\n");
  111. return -EIO;
  112. }
  113. /**
  114. * hl_pci_elbi_write() - Write through the ELBI interface.
  115. * @hdev: Pointer to hl_device structure.
  116. * @addr: Address to write to
  117. * @data: Data to write
  118. *
  119. * Return: 0 on success, negative value for failure.
  120. */
  121. static int hl_pci_elbi_write(struct hl_device *hdev, u64 addr, u32 data)
  122. {
  123. struct pci_dev *pdev = hdev->pdev;
  124. ktime_t timeout;
  125. u64 msec;
  126. u32 val;
  127. if (hdev->pldm)
  128. msec = HL_PLDM_PCI_ELBI_TIMEOUT_MSEC;
  129. else
  130. msec = HL_PCI_ELBI_TIMEOUT_MSEC;
  131. /* Clear previous status */
  132. pci_write_config_dword(pdev, mmPCI_CONFIG_ELBI_STS, 0);
  133. pci_write_config_dword(pdev, mmPCI_CONFIG_ELBI_ADDR, (u32) addr);
  134. pci_write_config_dword(pdev, mmPCI_CONFIG_ELBI_DATA, data);
  135. pci_write_config_dword(pdev, mmPCI_CONFIG_ELBI_CTRL,
  136. PCI_CONFIG_ELBI_CTRL_WRITE);
  137. timeout = ktime_add_ms(ktime_get(), msec);
  138. for (;;) {
  139. pci_read_config_dword(pdev, mmPCI_CONFIG_ELBI_STS, &val);
  140. if (val & PCI_CONFIG_ELBI_STS_MASK)
  141. break;
  142. if (ktime_compare(ktime_get(), timeout) > 0) {
  143. pci_read_config_dword(pdev, mmPCI_CONFIG_ELBI_STS,
  144. &val);
  145. break;
  146. }
  147. usleep_range(300, 500);
  148. }
  149. if ((val & PCI_CONFIG_ELBI_STS_MASK) == PCI_CONFIG_ELBI_STS_DONE)
  150. return 0;
  151. if (val & PCI_CONFIG_ELBI_STS_ERR)
  152. return -EIO;
  153. if (!(val & PCI_CONFIG_ELBI_STS_MASK)) {
  154. dev_err(hdev->dev, "ELBI write didn't finish in time\n");
  155. return -EIO;
  156. }
  157. dev_err(hdev->dev, "ELBI write has undefined bits in status\n");
  158. return -EIO;
  159. }
  160. /**
  161. * hl_pci_iatu_write() - iatu write routine.
  162. * @hdev: Pointer to hl_device structure.
  163. * @addr: Address to write to
  164. * @data: Data to write
  165. *
  166. * Return: 0 on success, negative value for failure.
  167. */
  168. int hl_pci_iatu_write(struct hl_device *hdev, u32 addr, u32 data)
  169. {
  170. struct asic_fixed_properties *prop = &hdev->asic_prop;
  171. u32 dbi_offset;
  172. int rc;
  173. dbi_offset = addr & 0xFFF;
  174. /* Ignore result of writing to pcie_aux_dbi_reg_addr as it could fail
  175. * in case the firmware security is enabled
  176. */
  177. hl_pci_elbi_write(hdev, prop->pcie_aux_dbi_reg_addr, 0x00300000);
  178. rc = hl_pci_elbi_write(hdev, prop->pcie_dbi_base_address + dbi_offset,
  179. data);
  180. if (rc)
  181. return -EIO;
  182. return 0;
  183. }
  184. /**
  185. * hl_pci_set_inbound_region() - Configure inbound region
  186. * @hdev: Pointer to hl_device structure.
  187. * @region: Inbound region number.
  188. * @pci_region: Inbound region parameters.
  189. *
  190. * Configure the iATU inbound region.
  191. *
  192. * Return: 0 on success, negative value for failure.
  193. */
  194. int hl_pci_set_inbound_region(struct hl_device *hdev, u8 region,
  195. struct hl_inbound_pci_region *pci_region)
  196. {
  197. struct asic_fixed_properties *prop = &hdev->asic_prop;
  198. u64 bar_phys_base, region_base, region_end_address;
  199. u32 offset, ctrl_reg_val;
  200. int rc = 0;
  201. /* region offset */
  202. offset = (0x200 * region) + 0x100;
  203. if (pci_region->mode == PCI_ADDRESS_MATCH_MODE) {
  204. bar_phys_base = hdev->pcie_bar_phys[pci_region->bar];
  205. region_base = bar_phys_base + pci_region->offset_in_bar;
  206. region_end_address = region_base + pci_region->size - 1;
  207. rc |= hl_pci_iatu_write(hdev, offset + 0x8,
  208. lower_32_bits(region_base));
  209. rc |= hl_pci_iatu_write(hdev, offset + 0xC,
  210. upper_32_bits(region_base));
  211. rc |= hl_pci_iatu_write(hdev, offset + 0x10,
  212. lower_32_bits(region_end_address));
  213. }
  214. /* Point to the specified address */
  215. rc |= hl_pci_iatu_write(hdev, offset + 0x14, lower_32_bits(pci_region->addr));
  216. rc |= hl_pci_iatu_write(hdev, offset + 0x18, upper_32_bits(pci_region->addr));
  217. /* Set bar type as memory */
  218. rc |= hl_pci_iatu_write(hdev, offset + 0x0, 0);
  219. /* Enable + bar/address match + match enable + bar number */
  220. ctrl_reg_val = FIELD_PREP(IATU_REGION_CTRL_REGION_EN_MASK, 1);
  221. ctrl_reg_val |= FIELD_PREP(IATU_REGION_CTRL_MATCH_MODE_MASK, pci_region->mode);
  222. ctrl_reg_val |= FIELD_PREP(IATU_REGION_CTRL_NUM_MATCH_EN_MASK, 1);
  223. if (pci_region->mode == PCI_BAR_MATCH_MODE)
  224. ctrl_reg_val |= FIELD_PREP(IATU_REGION_CTRL_BAR_NUM_MASK, pci_region->bar);
  225. rc |= hl_pci_iatu_write(hdev, offset + 0x4, ctrl_reg_val);
  226. /* Return the DBI window to the default location
  227. * Ignore result of writing to pcie_aux_dbi_reg_addr as it could fail
  228. * in case the firmware security is enabled
  229. */
  230. hl_pci_elbi_write(hdev, prop->pcie_aux_dbi_reg_addr, 0);
  231. if (rc)
  232. dev_err(hdev->dev, "failed to map bar %u to 0x%08llx\n",
  233. pci_region->bar, pci_region->addr);
  234. return rc;
  235. }
  236. /**
  237. * hl_pci_set_outbound_region() - Configure outbound region 0
  238. * @hdev: Pointer to hl_device structure.
  239. * @pci_region: Outbound region parameters.
  240. *
  241. * Configure the iATU outbound region 0.
  242. *
  243. * Return: 0 on success, negative value for failure.
  244. */
  245. int hl_pci_set_outbound_region(struct hl_device *hdev,
  246. struct hl_outbound_pci_region *pci_region)
  247. {
  248. struct asic_fixed_properties *prop = &hdev->asic_prop;
  249. u64 outbound_region_end_address;
  250. int rc = 0;
  251. /* Outbound Region 0 */
  252. outbound_region_end_address =
  253. pci_region->addr + pci_region->size - 1;
  254. rc |= hl_pci_iatu_write(hdev, 0x008,
  255. lower_32_bits(pci_region->addr));
  256. rc |= hl_pci_iatu_write(hdev, 0x00C,
  257. upper_32_bits(pci_region->addr));
  258. rc |= hl_pci_iatu_write(hdev, 0x010,
  259. lower_32_bits(outbound_region_end_address));
  260. rc |= hl_pci_iatu_write(hdev, 0x014, 0);
  261. rc |= hl_pci_iatu_write(hdev, 0x018, 0);
  262. rc |= hl_pci_iatu_write(hdev, 0x020,
  263. upper_32_bits(outbound_region_end_address));
  264. /* Increase region size */
  265. rc |= hl_pci_iatu_write(hdev, 0x000, 0x00002000);
  266. /* Enable */
  267. rc |= hl_pci_iatu_write(hdev, 0x004, 0x80000000);
  268. /* Return the DBI window to the default location
  269. * Ignore result of writing to pcie_aux_dbi_reg_addr as it could fail
  270. * in case the firmware security is enabled
  271. */
  272. hl_pci_elbi_write(hdev, prop->pcie_aux_dbi_reg_addr, 0);
  273. return rc;
  274. }
  275. /**
  276. * hl_get_pci_memory_region() - get PCI region for given address
  277. * @hdev: Pointer to hl_device structure.
  278. * @addr: device address
  279. *
  280. * @return region index on success, otherwise PCI_REGION_NUMBER (invalid
  281. * region index)
  282. */
  283. enum pci_region hl_get_pci_memory_region(struct hl_device *hdev, u64 addr)
  284. {
  285. int i;
  286. for (i = 0 ; i < PCI_REGION_NUMBER ; i++) {
  287. struct pci_mem_region *region = &hdev->pci_mem_region[i];
  288. if (!region->used)
  289. continue;
  290. if ((addr >= region->region_base) &&
  291. (addr < region->region_base + region->region_size))
  292. return i;
  293. }
  294. return PCI_REGION_NUMBER;
  295. }
  296. /**
  297. * hl_pci_init() - PCI initialization code.
  298. * @hdev: Pointer to hl_device structure.
  299. *
  300. * Set DMA masks, initialize the PCI controller and map the PCI BARs.
  301. *
  302. * Return: 0 on success, non-zero for failure.
  303. */
  304. int hl_pci_init(struct hl_device *hdev)
  305. {
  306. struct asic_fixed_properties *prop = &hdev->asic_prop;
  307. struct pci_dev *pdev = hdev->pdev;
  308. int rc;
  309. rc = pci_enable_device_mem(pdev);
  310. if (rc) {
  311. dev_err(hdev->dev, "can't enable PCI device\n");
  312. return rc;
  313. }
  314. pci_set_master(pdev);
  315. rc = hdev->asic_funcs->pci_bars_map(hdev);
  316. if (rc) {
  317. dev_err(hdev->dev, "Failed to map PCI BAR addresses\n");
  318. goto disable_device;
  319. }
  320. rc = hdev->asic_funcs->init_iatu(hdev);
  321. if (rc) {
  322. dev_err(hdev->dev, "PCI controller was not initialized successfully\n");
  323. goto unmap_pci_bars;
  324. }
  325. /* Driver must sleep in order for FW to finish the iATU configuration */
  326. if (hdev->asic_prop.iatu_done_by_fw)
  327. usleep_range(2000, 3000);
  328. rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(prop->dma_mask));
  329. if (rc) {
  330. dev_err(hdev->dev,
  331. "Failed to set dma mask to %d bits, error %d\n",
  332. prop->dma_mask, rc);
  333. goto unmap_pci_bars;
  334. }
  335. dma_set_max_seg_size(&pdev->dev, U32_MAX);
  336. return 0;
  337. unmap_pci_bars:
  338. hl_pci_bars_unmap(hdev);
  339. disable_device:
  340. pci_clear_master(pdev);
  341. pci_disable_device(pdev);
  342. return rc;
  343. }
  344. /**
  345. * hl_pci_fini() - PCI finalization code.
  346. * @hdev: Pointer to hl_device structure
  347. *
  348. * Unmap PCI bars and disable PCI device.
  349. */
  350. void hl_pci_fini(struct hl_device *hdev)
  351. {
  352. hl_pci_bars_unmap(hdev);
  353. pci_clear_master(hdev->pdev);
  354. pci_disable_device(hdev->pdev);
  355. }