1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright 2016-2019 HabanaLabs, Ltd.
8 #include "../habanalabs.h"
9 #include "../../include/hw_ip/pci/pci_general.h"
11 #include <linux/pci.h>
13 #include <trace/events/habanalabs.h>
15 #define HL_PLDM_PCI_ELBI_TIMEOUT_MSEC (HL_PCI_ELBI_TIMEOUT_MSEC * 100)
17 #define IATU_REGION_CTRL_REGION_EN_MASK BIT(31)
18 #define IATU_REGION_CTRL_MATCH_MODE_MASK BIT(30)
19 #define IATU_REGION_CTRL_NUM_MATCH_EN_MASK BIT(19)
20 #define IATU_REGION_CTRL_BAR_NUM_MASK GENMASK(10, 8)
23 * hl_pci_bars_map() - Map PCI BARs.
24 * @hdev: Pointer to hl_device structure.
25 * @name: Array of BAR names.
26 * @is_wc: Array with flag per BAR whether a write-combined mapping is needed.
28 * Request PCI regions and map them to kernel virtual addresses.
30 * Return: 0 on success, non-zero for failure.
32 int hl_pci_bars_map(struct hl_device *hdev, const char * const name[3],
35 struct pci_dev *pdev = hdev->pdev;
38 rc = pci_request_regions(pdev, HL_NAME);
40 dev_err(hdev->dev, "Cannot obtain PCI resources\n");
44 for (i = 0 ; i < 3 ; i++) {
45 bar = i * 2; /* 64-bit BARs */
46 hdev->pcie_bar[bar] = is_wc[i] ?
47 pci_ioremap_wc_bar(pdev, bar) :
48 pci_ioremap_bar(pdev, bar);
49 if (!hdev->pcie_bar[bar]) {
50 dev_err(hdev->dev, "pci_ioremap%s_bar failed for %s\n",
51 is_wc[i] ? "_wc" : "", name[i]);
60 for (i = 2 ; i >= 0 ; i--) {
61 bar = i * 2; /* 64-bit BARs */
62 if (hdev->pcie_bar[bar])
63 iounmap(hdev->pcie_bar[bar]);
66 pci_release_regions(pdev);
72 * hl_pci_bars_unmap() - Unmap PCI BARS.
73 * @hdev: Pointer to hl_device structure.
75 * Release all PCI BARs and unmap their virtual addresses.
77 static void hl_pci_bars_unmap(struct hl_device *hdev)
79 struct pci_dev *pdev = hdev->pdev;
82 for (i = 2 ; i >= 0 ; i--) {
83 bar = i * 2; /* 64-bit BARs */
84 iounmap(hdev->pcie_bar[bar]);
87 pci_release_regions(pdev);
90 int hl_pci_elbi_read(struct hl_device *hdev, u64 addr, u32 *data)
92 struct pci_dev *pdev = hdev->pdev;
98 msec = HL_PLDM_PCI_ELBI_TIMEOUT_MSEC;
100 msec = HL_PCI_ELBI_TIMEOUT_MSEC;
102 /* Clear previous status */
103 pci_write_config_dword(pdev, mmPCI_CONFIG_ELBI_STS, 0);
105 pci_write_config_dword(pdev, mmPCI_CONFIG_ELBI_ADDR, (u32) addr);
106 pci_write_config_dword(pdev, mmPCI_CONFIG_ELBI_CTRL, 0);
108 timeout = ktime_add_ms(ktime_get(), msec);
110 pci_read_config_dword(pdev, mmPCI_CONFIG_ELBI_STS, &val);
111 if (val & PCI_CONFIG_ELBI_STS_MASK)
113 if (ktime_compare(ktime_get(), timeout) > 0) {
114 pci_read_config_dword(pdev, mmPCI_CONFIG_ELBI_STS,
119 usleep_range(300, 500);
122 if ((val & PCI_CONFIG_ELBI_STS_MASK) == PCI_CONFIG_ELBI_STS_DONE) {
123 pci_read_config_dword(pdev, mmPCI_CONFIG_ELBI_DATA, data);
125 if (unlikely(trace_habanalabs_elbi_read_enabled()))
126 trace_habanalabs_elbi_read(hdev->dev, (u32) addr, val);
131 if (val & PCI_CONFIG_ELBI_STS_ERR) {
132 dev_err(hdev->dev, "Error reading from ELBI\n");
136 if (!(val & PCI_CONFIG_ELBI_STS_MASK)) {
137 dev_err(hdev->dev, "ELBI read didn't finish in time\n");
141 dev_err(hdev->dev, "ELBI read has undefined bits in status\n");
146 * hl_pci_elbi_write() - Write through the ELBI interface.
147 * @hdev: Pointer to hl_device structure.
148 * @addr: Address to write to
149 * @data: Data to write
151 * Return: 0 on success, negative value for failure.
153 static int hl_pci_elbi_write(struct hl_device *hdev, u64 addr, u32 data)
155 struct pci_dev *pdev = hdev->pdev;
161 msec = HL_PLDM_PCI_ELBI_TIMEOUT_MSEC;
163 msec = HL_PCI_ELBI_TIMEOUT_MSEC;
165 /* Clear previous status */
166 pci_write_config_dword(pdev, mmPCI_CONFIG_ELBI_STS, 0);
168 pci_write_config_dword(pdev, mmPCI_CONFIG_ELBI_ADDR, (u32) addr);
169 pci_write_config_dword(pdev, mmPCI_CONFIG_ELBI_DATA, data);
170 pci_write_config_dword(pdev, mmPCI_CONFIG_ELBI_CTRL,
171 PCI_CONFIG_ELBI_CTRL_WRITE);
173 timeout = ktime_add_ms(ktime_get(), msec);
175 pci_read_config_dword(pdev, mmPCI_CONFIG_ELBI_STS, &val);
176 if (val & PCI_CONFIG_ELBI_STS_MASK)
178 if (ktime_compare(ktime_get(), timeout) > 0) {
179 pci_read_config_dword(pdev, mmPCI_CONFIG_ELBI_STS,
184 usleep_range(300, 500);
187 if ((val & PCI_CONFIG_ELBI_STS_MASK) == PCI_CONFIG_ELBI_STS_DONE) {
188 if (unlikely(trace_habanalabs_elbi_write_enabled()))
189 trace_habanalabs_elbi_write(hdev->dev, (u32) addr, val);
193 if (val & PCI_CONFIG_ELBI_STS_ERR)
196 if (!(val & PCI_CONFIG_ELBI_STS_MASK)) {
197 dev_err(hdev->dev, "ELBI write didn't finish in time\n");
201 dev_err(hdev->dev, "ELBI write has undefined bits in status\n");
206 * hl_pci_iatu_write() - iatu write routine.
207 * @hdev: Pointer to hl_device structure.
208 * @addr: Address to write to
209 * @data: Data to write
211 * Return: 0 on success, negative value for failure.
213 int hl_pci_iatu_write(struct hl_device *hdev, u32 addr, u32 data)
215 struct asic_fixed_properties *prop = &hdev->asic_prop;
219 dbi_offset = addr & 0xFFF;
221 /* Ignore result of writing to pcie_aux_dbi_reg_addr as it could fail
222 * in case the firmware security is enabled
224 hl_pci_elbi_write(hdev, prop->pcie_aux_dbi_reg_addr, 0x00300000);
226 rc = hl_pci_elbi_write(hdev, prop->pcie_dbi_base_address + dbi_offset,
236 * hl_pci_set_inbound_region() - Configure inbound region
237 * @hdev: Pointer to hl_device structure.
238 * @region: Inbound region number.
239 * @pci_region: Inbound region parameters.
241 * Configure the iATU inbound region.
243 * Return: 0 on success, negative value for failure.
245 int hl_pci_set_inbound_region(struct hl_device *hdev, u8 region,
246 struct hl_inbound_pci_region *pci_region)
248 struct asic_fixed_properties *prop = &hdev->asic_prop;
249 u64 bar_phys_base, region_base, region_end_address;
250 u32 offset, ctrl_reg_val;
254 offset = (0x200 * region) + 0x100;
256 if (pci_region->mode == PCI_ADDRESS_MATCH_MODE) {
257 bar_phys_base = hdev->pcie_bar_phys[pci_region->bar];
258 region_base = bar_phys_base + pci_region->offset_in_bar;
259 region_end_address = region_base + pci_region->size - 1;
261 rc |= hl_pci_iatu_write(hdev, offset + 0x8,
262 lower_32_bits(region_base));
263 rc |= hl_pci_iatu_write(hdev, offset + 0xC,
264 upper_32_bits(region_base));
265 rc |= hl_pci_iatu_write(hdev, offset + 0x10,
266 lower_32_bits(region_end_address));
269 /* Point to the specified address */
270 rc |= hl_pci_iatu_write(hdev, offset + 0x14, lower_32_bits(pci_region->addr));
271 rc |= hl_pci_iatu_write(hdev, offset + 0x18, upper_32_bits(pci_region->addr));
273 /* Set bar type as memory */
274 rc |= hl_pci_iatu_write(hdev, offset + 0x0, 0);
276 /* Enable + bar/address match + match enable + bar number */
277 ctrl_reg_val = FIELD_PREP(IATU_REGION_CTRL_REGION_EN_MASK, 1);
278 ctrl_reg_val |= FIELD_PREP(IATU_REGION_CTRL_MATCH_MODE_MASK, pci_region->mode);
279 ctrl_reg_val |= FIELD_PREP(IATU_REGION_CTRL_NUM_MATCH_EN_MASK, 1);
281 if (pci_region->mode == PCI_BAR_MATCH_MODE)
282 ctrl_reg_val |= FIELD_PREP(IATU_REGION_CTRL_BAR_NUM_MASK, pci_region->bar);
284 rc |= hl_pci_iatu_write(hdev, offset + 0x4, ctrl_reg_val);
286 /* Return the DBI window to the default location
287 * Ignore result of writing to pcie_aux_dbi_reg_addr as it could fail
288 * in case the firmware security is enabled
290 hl_pci_elbi_write(hdev, prop->pcie_aux_dbi_reg_addr, 0);
293 dev_err(hdev->dev, "failed to map bar %u to 0x%08llx\n",
294 pci_region->bar, pci_region->addr);
300 * hl_pci_set_outbound_region() - Configure outbound region 0
301 * @hdev: Pointer to hl_device structure.
302 * @pci_region: Outbound region parameters.
304 * Configure the iATU outbound region 0.
306 * Return: 0 on success, negative value for failure.
308 int hl_pci_set_outbound_region(struct hl_device *hdev,
309 struct hl_outbound_pci_region *pci_region)
311 struct asic_fixed_properties *prop = &hdev->asic_prop;
312 u64 outbound_region_end_address;
315 /* Outbound Region 0 */
316 outbound_region_end_address =
317 pci_region->addr + pci_region->size - 1;
318 rc |= hl_pci_iatu_write(hdev, 0x008,
319 lower_32_bits(pci_region->addr));
320 rc |= hl_pci_iatu_write(hdev, 0x00C,
321 upper_32_bits(pci_region->addr));
322 rc |= hl_pci_iatu_write(hdev, 0x010,
323 lower_32_bits(outbound_region_end_address));
324 rc |= hl_pci_iatu_write(hdev, 0x014, 0);
326 rc |= hl_pci_iatu_write(hdev, 0x018, 0);
328 rc |= hl_pci_iatu_write(hdev, 0x020,
329 upper_32_bits(outbound_region_end_address));
330 /* Increase region size */
331 rc |= hl_pci_iatu_write(hdev, 0x000, 0x00002000);
333 rc |= hl_pci_iatu_write(hdev, 0x004, 0x80000000);
335 /* Return the DBI window to the default location
336 * Ignore result of writing to pcie_aux_dbi_reg_addr as it could fail
337 * in case the firmware security is enabled
339 hl_pci_elbi_write(hdev, prop->pcie_aux_dbi_reg_addr, 0);
345 * hl_get_pci_memory_region() - get PCI region for given address
346 * @hdev: Pointer to hl_device structure.
347 * @addr: device address
349 * @return region index on success, otherwise PCI_REGION_NUMBER (invalid
352 enum pci_region hl_get_pci_memory_region(struct hl_device *hdev, u64 addr)
356 for (i = 0 ; i < PCI_REGION_NUMBER ; i++) {
357 struct pci_mem_region *region = &hdev->pci_mem_region[i];
362 if ((addr >= region->region_base) &&
363 (addr < region->region_base + region->region_size))
367 return PCI_REGION_NUMBER;
371 * hl_pci_init() - PCI initialization code.
372 * @hdev: Pointer to hl_device structure.
374 * Set DMA masks, initialize the PCI controller and map the PCI BARs.
376 * Return: 0 on success, non-zero for failure.
378 int hl_pci_init(struct hl_device *hdev)
380 struct asic_fixed_properties *prop = &hdev->asic_prop;
381 struct pci_dev *pdev = hdev->pdev;
384 rc = pci_enable_device_mem(pdev);
386 dev_err(hdev->dev, "can't enable PCI device\n");
390 pci_set_master(pdev);
392 rc = hdev->asic_funcs->pci_bars_map(hdev);
394 dev_err(hdev->dev, "Failed to map PCI BAR addresses\n");
398 rc = hdev->asic_funcs->init_iatu(hdev);
400 dev_err(hdev->dev, "PCI controller was not initialized successfully\n");
404 /* Driver must sleep in order for FW to finish the iATU configuration */
405 if (hdev->asic_prop.iatu_done_by_fw)
406 usleep_range(2000, 3000);
408 rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(prop->dma_mask));
411 "Failed to set dma mask to %d bits, error %d\n",
416 dma_set_max_seg_size(&pdev->dev, U32_MAX);
421 hl_pci_bars_unmap(hdev);
423 pci_disable_device(pdev);
429 * hl_pci_fini() - PCI finalization code.
430 * @hdev: Pointer to hl_device structure
432 * Unmap PCI bars and disable PCI device.
434 void hl_pci_fini(struct hl_device *hdev)
436 hl_pci_bars_unmap(hdev);
438 pci_disable_device(hdev->pdev);