blob: 7bd3737571f38acabc08581b0246e016e818d970 [file] [log] [blame]
Tomer Tayarb6f897d2019-03-05 16:48:42 +02001// SPDX-License-Identifier: GPL-2.0
2
3/*
4 * Copyright 2016-2019 HabanaLabs, Ltd.
5 * All Rights Reserved.
6 */
7
8#include "habanalabs.h"
Greg Kroah-Hartman7b16a152020-07-28 19:18:51 +02009#include "../include/hw_ip/pci/pci_general.h"
Tomer Tayarb6f897d2019-03-05 16:48:42 +020010
11#include <linux/pci.h>
Ofir Bittonf4cbfd22020-06-15 17:45:12 +030012#include <linux/bitfield.h>
Tomer Tayarb6f897d2019-03-05 16:48:42 +020013
Omer Shpigelmana1e537b2019-05-13 14:44:50 +030014#define HL_PLDM_PCI_ELBI_TIMEOUT_MSEC (HL_PCI_ELBI_TIMEOUT_MSEC * 10)
15
Ofir Bittonf4cbfd22020-06-15 17:45:12 +030016#define IATU_REGION_CTRL_REGION_EN_MASK BIT(31)
17#define IATU_REGION_CTRL_MATCH_MODE_MASK BIT(30)
18#define IATU_REGION_CTRL_NUM_MATCH_EN_MASK BIT(19)
19#define IATU_REGION_CTRL_BAR_NUM_MASK GENMASK(10, 8)
20
Tomer Tayarb6f897d2019-03-05 16:48:42 +020021/**
22 * hl_pci_bars_map() - Map PCI BARs.
23 * @hdev: Pointer to hl_device structure.
Lee Jonesdf123c92020-07-01 09:58:37 +010024 * @name: Array of BAR names.
Tomer Tayarb6f897d2019-03-05 16:48:42 +020025 * @is_wc: Array with flag per BAR whether a write-combined mapping is needed.
26 *
27 * Request PCI regions and map them to kernel virtual addresses.
28 *
29 * Return: 0 on success, non-zero for failure.
30 */
31int hl_pci_bars_map(struct hl_device *hdev, const char * const name[3],
32 bool is_wc[3])
33{
34 struct pci_dev *pdev = hdev->pdev;
35 int rc, i, bar;
36
37 rc = pci_request_regions(pdev, HL_NAME);
38 if (rc) {
39 dev_err(hdev->dev, "Cannot obtain PCI resources\n");
40 return rc;
41 }
42
43 for (i = 0 ; i < 3 ; i++) {
44 bar = i * 2; /* 64-bit BARs */
45 hdev->pcie_bar[bar] = is_wc[i] ?
46 pci_ioremap_wc_bar(pdev, bar) :
47 pci_ioremap_bar(pdev, bar);
48 if (!hdev->pcie_bar[bar]) {
49 dev_err(hdev->dev, "pci_ioremap%s_bar failed for %s\n",
50 is_wc[i] ? "_wc" : "", name[i]);
51 rc = -ENODEV;
52 goto err;
53 }
54 }
55
56 return 0;
57
58err:
59 for (i = 2 ; i >= 0 ; i--) {
60 bar = i * 2; /* 64-bit BARs */
61 if (hdev->pcie_bar[bar])
62 iounmap(hdev->pcie_bar[bar]);
63 }
64
65 pci_release_regions(pdev);
66
67 return rc;
68}
69
Lee Jonesdf123c92020-07-01 09:58:37 +010070/**
Tomer Tayarb6f897d2019-03-05 16:48:42 +020071 * hl_pci_bars_unmap() - Unmap PCI BARS.
72 * @hdev: Pointer to hl_device structure.
73 *
74 * Release all PCI BARs and unmap their virtual addresses.
75 */
76static void hl_pci_bars_unmap(struct hl_device *hdev)
77{
78 struct pci_dev *pdev = hdev->pdev;
79 int i, bar;
80
81 for (i = 2 ; i >= 0 ; i--) {
82 bar = i * 2; /* 64-bit BARs */
83 iounmap(hdev->pcie_bar[bar]);
84 }
85
86 pci_release_regions(pdev);
87}
88
Lee Jonesdf123c92020-07-01 09:58:37 +010089/**
Tomer Tayarb6f897d2019-03-05 16:48:42 +020090 * hl_pci_elbi_write() - Write through the ELBI interface.
91 * @hdev: Pointer to hl_device structure.
Lee Jonesdf123c92020-07-01 09:58:37 +010092 * @addr: Address to write to
93 * @data: Data to write
Tomer Tayarb6f897d2019-03-05 16:48:42 +020094 *
95 * Return: 0 on success, negative value for failure.
96 */
97static int hl_pci_elbi_write(struct hl_device *hdev, u64 addr, u32 data)
98{
99 struct pci_dev *pdev = hdev->pdev;
100 ktime_t timeout;
Omer Shpigelmana1e537b2019-05-13 14:44:50 +0300101 u64 msec;
Tomer Tayarb6f897d2019-03-05 16:48:42 +0200102 u32 val;
103
Omer Shpigelmana1e537b2019-05-13 14:44:50 +0300104 if (hdev->pldm)
105 msec = HL_PLDM_PCI_ELBI_TIMEOUT_MSEC;
106 else
107 msec = HL_PCI_ELBI_TIMEOUT_MSEC;
108
Tomer Tayarb6f897d2019-03-05 16:48:42 +0200109 /* Clear previous status */
110 pci_write_config_dword(pdev, mmPCI_CONFIG_ELBI_STS, 0);
111
112 pci_write_config_dword(pdev, mmPCI_CONFIG_ELBI_ADDR, (u32) addr);
113 pci_write_config_dword(pdev, mmPCI_CONFIG_ELBI_DATA, data);
114 pci_write_config_dword(pdev, mmPCI_CONFIG_ELBI_CTRL,
115 PCI_CONFIG_ELBI_CTRL_WRITE);
116
Omer Shpigelmana1e537b2019-05-13 14:44:50 +0300117 timeout = ktime_add_ms(ktime_get(), msec);
Tomer Tayarb6f897d2019-03-05 16:48:42 +0200118 for (;;) {
119 pci_read_config_dword(pdev, mmPCI_CONFIG_ELBI_STS, &val);
120 if (val & PCI_CONFIG_ELBI_STS_MASK)
121 break;
122 if (ktime_compare(ktime_get(), timeout) > 0) {
123 pci_read_config_dword(pdev, mmPCI_CONFIG_ELBI_STS,
124 &val);
125 break;
126 }
127
128 usleep_range(300, 500);
129 }
130
131 if ((val & PCI_CONFIG_ELBI_STS_MASK) == PCI_CONFIG_ELBI_STS_DONE)
132 return 0;
133
134 if (val & PCI_CONFIG_ELBI_STS_ERR) {
135 dev_err(hdev->dev, "Error writing to ELBI\n");
136 return -EIO;
137 }
138
139 if (!(val & PCI_CONFIG_ELBI_STS_MASK)) {
140 dev_err(hdev->dev, "ELBI write didn't finish in time\n");
141 return -EIO;
142 }
143
144 dev_err(hdev->dev, "ELBI write has undefined bits in status\n");
145 return -EIO;
146}
147
148/**
149 * hl_pci_iatu_write() - iatu write routine.
150 * @hdev: Pointer to hl_device structure.
Lee Jonesdf123c92020-07-01 09:58:37 +0100151 * @addr: Address to write to
152 * @data: Data to write
Tomer Tayarb6f897d2019-03-05 16:48:42 +0200153 *
154 * Return: 0 on success, negative value for failure.
155 */
156int hl_pci_iatu_write(struct hl_device *hdev, u32 addr, u32 data)
157{
158 struct asic_fixed_properties *prop = &hdev->asic_prop;
159 u32 dbi_offset;
160 int rc;
161
162 dbi_offset = addr & 0xFFF;
163
164 rc = hl_pci_elbi_write(hdev, prop->pcie_aux_dbi_reg_addr, 0x00300000);
165 rc |= hl_pci_elbi_write(hdev, prop->pcie_dbi_base_address + dbi_offset,
166 data);
167
168 if (rc)
169 return -EIO;
170
171 return 0;
172}
173
Lee Jonesdf123c92020-07-01 09:58:37 +0100174/**
Tomer Tayarb6f897d2019-03-05 16:48:42 +0200175 * hl_pci_reset_link_through_bridge() - Reset PCI link.
176 * @hdev: Pointer to hl_device structure.
177 */
178static void hl_pci_reset_link_through_bridge(struct hl_device *hdev)
179{
180 struct pci_dev *pdev = hdev->pdev;
181 struct pci_dev *parent_port;
182 u16 val;
183
184 parent_port = pdev->bus->self;
185 pci_read_config_word(parent_port, PCI_BRIDGE_CONTROL, &val);
186 val |= PCI_BRIDGE_CTL_BUS_RESET;
187 pci_write_config_word(parent_port, PCI_BRIDGE_CONTROL, val);
188 ssleep(1);
189
190 val &= ~(PCI_BRIDGE_CTL_BUS_RESET);
191 pci_write_config_word(parent_port, PCI_BRIDGE_CONTROL, val);
192 ssleep(3);
193}
194
195/**
Ofir Bittonf4cbfd22020-06-15 17:45:12 +0300196 * hl_pci_set_inbound_region() - Configure inbound region
Tomer Tayarb6f897d2019-03-05 16:48:42 +0200197 * @hdev: Pointer to hl_device structure.
Ofir Bittonf4cbfd22020-06-15 17:45:12 +0300198 * @region: Inbound region number.
199 * @pci_region: Inbound region parameters.
Tomer Tayarb6f897d2019-03-05 16:48:42 +0200200 *
Ofir Bittonf4cbfd22020-06-15 17:45:12 +0300201 * Configure the iATU inbound region.
Tomer Tayarb6f897d2019-03-05 16:48:42 +0200202 *
203 * Return: 0 on success, negative value for failure.
204 */
Ofir Bittonf4cbfd22020-06-15 17:45:12 +0300205int hl_pci_set_inbound_region(struct hl_device *hdev, u8 region,
206 struct hl_inbound_pci_region *pci_region)
Tomer Tayarb6f897d2019-03-05 16:48:42 +0200207{
208 struct asic_fixed_properties *prop = &hdev->asic_prop;
Ofir Bittonf4cbfd22020-06-15 17:45:12 +0300209 u64 bar_phys_base, region_base, region_end_address;
210 u32 offset, ctrl_reg_val;
211 int rc = 0;
Tomer Tayarb6f897d2019-03-05 16:48:42 +0200212
Ofir Bittonf4cbfd22020-06-15 17:45:12 +0300213 /* region offset */
214 offset = (0x200 * region) + 0x100;
Tomer Tayarb6f897d2019-03-05 16:48:42 +0200215
Ofir Bittonf4cbfd22020-06-15 17:45:12 +0300216 if (pci_region->mode == PCI_ADDRESS_MATCH_MODE) {
217 bar_phys_base = hdev->pcie_bar_phys[pci_region->bar];
218 region_base = bar_phys_base + pci_region->offset_in_bar;
219 region_end_address = region_base + pci_region->size - 1;
220
221 rc |= hl_pci_iatu_write(hdev, offset + 0x8,
222 lower_32_bits(region_base));
223 rc |= hl_pci_iatu_write(hdev, offset + 0xC,
224 upper_32_bits(region_base));
225 rc |= hl_pci_iatu_write(hdev, offset + 0x10,
226 lower_32_bits(region_end_address));
Tomer Tayarb6f897d2019-03-05 16:48:42 +0200227 }
228
229 /* Point to the specified address */
Ofir Bittonf4cbfd22020-06-15 17:45:12 +0300230 rc = hl_pci_iatu_write(hdev, offset + 0x14,
231 lower_32_bits(pci_region->addr));
232 rc |= hl_pci_iatu_write(hdev, offset + 0x18,
233 upper_32_bits(pci_region->addr));
Tomer Tayarb6f897d2019-03-05 16:48:42 +0200234 rc |= hl_pci_iatu_write(hdev, offset + 0x0, 0);
Ofir Bittonf4cbfd22020-06-15 17:45:12 +0300235
236 /* Enable + bar/address match + match enable + bar number */
237 ctrl_reg_val = FIELD_PREP(IATU_REGION_CTRL_REGION_EN_MASK, 1);
238 ctrl_reg_val |= FIELD_PREP(IATU_REGION_CTRL_MATCH_MODE_MASK,
239 pci_region->mode);
240 ctrl_reg_val |= FIELD_PREP(IATU_REGION_CTRL_NUM_MATCH_EN_MASK, 1);
241
242 if (pci_region->mode == PCI_BAR_MATCH_MODE)
243 ctrl_reg_val |= FIELD_PREP(IATU_REGION_CTRL_BAR_NUM_MASK,
244 pci_region->bar);
245
246 rc |= hl_pci_iatu_write(hdev, offset + 0x4, ctrl_reg_val);
Tomer Tayarb6f897d2019-03-05 16:48:42 +0200247
248 /* Return the DBI window to the default location */
249 rc |= hl_pci_elbi_write(hdev, prop->pcie_aux_dbi_reg_addr, 0);
250 rc |= hl_pci_elbi_write(hdev, prop->pcie_aux_dbi_reg_addr + 4, 0);
251
252 if (rc)
Ofir Bittonf4cbfd22020-06-15 17:45:12 +0300253 dev_err(hdev->dev, "failed to map bar %u to 0x%08llx\n",
254 pci_region->bar, pci_region->addr);
Tomer Tayarb6f897d2019-03-05 16:48:42 +0200255
256 return rc;
257}
258
259/**
Ofir Bittonf4cbfd22020-06-15 17:45:12 +0300260 * hl_pci_set_outbound_region() - Configure outbound region 0
Tomer Tayarb6f897d2019-03-05 16:48:42 +0200261 * @hdev: Pointer to hl_device structure.
Ofir Bittonf4cbfd22020-06-15 17:45:12 +0300262 * @pci_region: Outbound region parameters.
Tomer Tayarb6f897d2019-03-05 16:48:42 +0200263 *
Ofir Bittonf4cbfd22020-06-15 17:45:12 +0300264 * Configure the iATU outbound region 0.
Tomer Tayarb6f897d2019-03-05 16:48:42 +0200265 *
266 * Return: 0 on success, negative value for failure.
267 */
Ofir Bittonf4cbfd22020-06-15 17:45:12 +0300268int hl_pci_set_outbound_region(struct hl_device *hdev,
269 struct hl_outbound_pci_region *pci_region)
Tomer Tayarb6f897d2019-03-05 16:48:42 +0200270{
271 struct asic_fixed_properties *prop = &hdev->asic_prop;
Ofir Bittonf4cbfd22020-06-15 17:45:12 +0300272 u64 outbound_region_end_address;
Tomer Tayarb6f897d2019-03-05 16:48:42 +0200273 int rc = 0;
274
Ofir Bittonf4cbfd22020-06-15 17:45:12 +0300275 /* Outbound Region 0 */
276 outbound_region_end_address =
277 pci_region->addr + pci_region->size - 1;
Tomer Tayarb6f897d2019-03-05 16:48:42 +0200278 rc |= hl_pci_iatu_write(hdev, 0x008,
Ofir Bittonf4cbfd22020-06-15 17:45:12 +0300279 lower_32_bits(pci_region->addr));
Tomer Tayarb6f897d2019-03-05 16:48:42 +0200280 rc |= hl_pci_iatu_write(hdev, 0x00C,
Ofir Bittonf4cbfd22020-06-15 17:45:12 +0300281 upper_32_bits(pci_region->addr));
282 rc |= hl_pci_iatu_write(hdev, 0x010,
283 lower_32_bits(outbound_region_end_address));
Tomer Tayarb6f897d2019-03-05 16:48:42 +0200284 rc |= hl_pci_iatu_write(hdev, 0x014, 0);
Oded Gabbaycb056b92020-03-29 13:18:30 +0300285
286 if ((hdev->power9_64bit_dma_enable) && (hdev->dma_mask == 64))
287 rc |= hl_pci_iatu_write(hdev, 0x018, 0x08000000);
288 else
289 rc |= hl_pci_iatu_write(hdev, 0x018, 0);
290
Ofir Bittonf4cbfd22020-06-15 17:45:12 +0300291 rc |= hl_pci_iatu_write(hdev, 0x020,
292 upper_32_bits(outbound_region_end_address));
Tomer Tayarb6f897d2019-03-05 16:48:42 +0200293 /* Increase region size */
294 rc |= hl_pci_iatu_write(hdev, 0x000, 0x00002000);
295 /* Enable */
296 rc |= hl_pci_iatu_write(hdev, 0x004, 0x80000000);
297
298 /* Return the DBI window to the default location */
299 rc |= hl_pci_elbi_write(hdev, prop->pcie_aux_dbi_reg_addr, 0);
300 rc |= hl_pci_elbi_write(hdev, prop->pcie_aux_dbi_reg_addr + 4, 0);
301
Ofir Bittonf4cbfd22020-06-15 17:45:12 +0300302 return rc;
Tomer Tayarb6f897d2019-03-05 16:48:42 +0200303}
304
305/**
Oded Gabbayd9973872019-03-07 18:03:23 +0200306 * hl_pci_set_dma_mask() - Set DMA masks for the device.
Tomer Tayarb6f897d2019-03-05 16:48:42 +0200307 * @hdev: Pointer to hl_device structure.
308 *
Oded Gabbayd9973872019-03-07 18:03:23 +0200309 * This function sets the DMA masks (regular and consistent) for a specified
310 * value. If it doesn't succeed, it tries to set it to a fall-back value
Tomer Tayarb6f897d2019-03-05 16:48:42 +0200311 *
312 * Return: 0 on success, non-zero for failure.
313 */
kbuild test robotba7193c2020-04-06 00:01:31 +0800314static int hl_pci_set_dma_mask(struct hl_device *hdev)
Tomer Tayarb6f897d2019-03-05 16:48:42 +0200315{
316 struct pci_dev *pdev = hdev->pdev;
317 int rc;
318
319 /* set DMA mask */
Oded Gabbaycb056b92020-03-29 13:18:30 +0300320 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(hdev->dma_mask));
Tomer Tayarb6f897d2019-03-05 16:48:42 +0200321 if (rc) {
Oded Gabbaycb056b92020-03-29 13:18:30 +0300322 dev_err(hdev->dev,
Oded Gabbayd9973872019-03-07 18:03:23 +0200323 "Failed to set pci dma mask to %d bits, error %d\n",
Oded Gabbaycb056b92020-03-29 13:18:30 +0300324 hdev->dma_mask, rc);
325 return rc;
Tomer Tayarb6f897d2019-03-05 16:48:42 +0200326 }
327
Oded Gabbaycb056b92020-03-29 13:18:30 +0300328 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(hdev->dma_mask));
Tomer Tayarb6f897d2019-03-05 16:48:42 +0200329 if (rc) {
Oded Gabbayd9973872019-03-07 18:03:23 +0200330 dev_err(hdev->dev,
331 "Failed to set pci consistent dma mask to %d bits, error %d\n",
Oded Gabbaycb056b92020-03-29 13:18:30 +0300332 hdev->dma_mask, rc);
Oded Gabbayd9973872019-03-07 18:03:23 +0200333 return rc;
Tomer Tayarb6f897d2019-03-05 16:48:42 +0200334 }
335
Oded Gabbayd9973872019-03-07 18:03:23 +0200336 return 0;
337}
338
339/**
340 * hl_pci_init() - PCI initialization code.
341 * @hdev: Pointer to hl_device structure.
Oded Gabbayd9973872019-03-07 18:03:23 +0200342 *
343 * Set DMA masks, initialize the PCI controller and map the PCI BARs.
344 *
345 * Return: 0 on success, non-zero for failure.
346 */
Oded Gabbaycb056b92020-03-29 13:18:30 +0300347int hl_pci_init(struct hl_device *hdev)
Oded Gabbayd9973872019-03-07 18:03:23 +0200348{
349 struct pci_dev *pdev = hdev->pdev;
350 int rc;
351
Tomer Tayarb6f897d2019-03-05 16:48:42 +0200352 if (hdev->reset_pcilink)
353 hl_pci_reset_link_through_bridge(hdev);
354
355 rc = pci_enable_device_mem(pdev);
356 if (rc) {
357 dev_err(hdev->dev, "can't enable PCI device\n");
358 return rc;
359 }
360
361 pci_set_master(pdev);
362
Oded Gabbaycb056b92020-03-29 13:18:30 +0300363 rc = hdev->asic_funcs->pci_bars_map(hdev);
364 if (rc) {
365 dev_err(hdev->dev, "Failed to initialize PCI BARs\n");
366 goto disable_device;
367 }
368
Tomer Tayarb6f897d2019-03-05 16:48:42 +0200369 rc = hdev->asic_funcs->init_iatu(hdev);
370 if (rc) {
371 dev_err(hdev->dev, "Failed to initialize iATU\n");
372 goto disable_device;
373 }
374
Oded Gabbaycb056b92020-03-29 13:18:30 +0300375 rc = hl_pci_set_dma_mask(hdev);
376 if (rc)
Tomer Tayarb6f897d2019-03-05 16:48:42 +0200377 goto disable_device;
Tomer Tayarb6f897d2019-03-05 16:48:42 +0200378
379 return 0;
380
381disable_device:
382 pci_clear_master(pdev);
383 pci_disable_device(pdev);
384
385 return rc;
386}
387
388/**
389 * hl_fw_fini() - PCI finalization code.
390 * @hdev: Pointer to hl_device structure
391 *
392 * Unmap PCI bars and disable PCI device.
393 */
394void hl_pci_fini(struct hl_device *hdev)
395{
396 hl_pci_bars_unmap(hdev);
397
398 pci_clear_master(hdev->pdev);
399 pci_disable_device(hdev->pdev);
400}