1 // SPDX-License-Identifier: MIT
3 * Copyright 2022 Advanced Micro Devices, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
28 * amdgpu_mm_rdoorbell - read a doorbell dword
30 * @adev: amdgpu_device pointer
31 * @index: doorbell index
33 * Returns the value in the doorbell aperture at the
34 * requested doorbell index (CIK).
36 u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
38 if (amdgpu_device_skip_hw_access(adev))
41 if (index < adev->doorbell.num_kernel_doorbells)
42 return readl(adev->doorbell.ptr + index);
44 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
49 * amdgpu_mm_wdoorbell - write a doorbell dword
51 * @adev: amdgpu_device pointer
52 * @index: doorbell index
55 * Writes @v to the doorbell aperture at the
56 * requested doorbell index (CIK).
58 void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
60 if (amdgpu_device_skip_hw_access(adev))
63 if (index < adev->doorbell.num_kernel_doorbells)
64 writel(v, adev->doorbell.ptr + index);
66 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
70 * amdgpu_mm_rdoorbell64 - read a doorbell Qword
72 * @adev: amdgpu_device pointer
73 * @index: doorbell index
75 * Returns the value in the doorbell aperture at the
76 * requested doorbell index (VEGA10+).
78 u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev, u32 index)
80 if (amdgpu_device_skip_hw_access(adev))
83 if (index < adev->doorbell.num_kernel_doorbells)
84 return atomic64_read((atomic64_t *)(adev->doorbell.ptr + index));
86 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
91 * amdgpu_mm_wdoorbell64 - write a doorbell Qword
93 * @adev: amdgpu_device pointer
94 * @index: doorbell index
97 * Writes @v to the doorbell aperture at the
98 * requested doorbell index (VEGA10+).
100 void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
102 if (amdgpu_device_skip_hw_access(adev))
105 if (index < adev->doorbell.num_kernel_doorbells)
106 atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v);
108 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
112 * GPU doorbell aperture helpers function.
115 * amdgpu_doorbell_init - Init doorbell driver information.
117 * @adev: amdgpu_device pointer
119 * Init doorbell driver information (CIK)
120 * Returns 0 on success, error on failure.
122 int amdgpu_doorbell_init(struct amdgpu_device *adev)
125 /* No doorbell on SI hardware generation */
126 if (adev->asic_type < CHIP_BONAIRE) {
127 adev->doorbell.base = 0;
128 adev->doorbell.size = 0;
129 adev->doorbell.num_kernel_doorbells = 0;
130 adev->doorbell.ptr = NULL;
134 if (pci_resource_flags(adev->pdev, 2) & IORESOURCE_UNSET)
137 amdgpu_asic_init_doorbell_index(adev);
139 /* doorbell bar mapping */
140 adev->doorbell.base = pci_resource_start(adev->pdev, 2);
141 adev->doorbell.size = pci_resource_len(adev->pdev, 2);
143 if (adev->enable_mes) {
144 adev->doorbell.num_kernel_doorbells =
145 adev->doorbell.size / sizeof(u32);
147 adev->doorbell.num_kernel_doorbells =
148 min_t(u32, adev->doorbell.size / sizeof(u32),
149 adev->doorbell_index.max_assignment+1);
150 if (adev->doorbell.num_kernel_doorbells == 0)
153 /* For Vega, reserve and map two pages on doorbell BAR since SDMA
154 * paging queue doorbell use the second page. The
155 * AMDGPU_DOORBELL64_MAX_ASSIGNMENT definition assumes all the
156 * doorbells are in the first page. So with paging queue enabled,
157 * the max num_kernel_doorbells should + 1 page (0x400 in dword)
159 if (adev->asic_type >= CHIP_VEGA10)
160 adev->doorbell.num_kernel_doorbells += 0x400;
163 adev->doorbell.ptr = ioremap(adev->doorbell.base,
164 adev->doorbell.num_kernel_doorbells *
166 if (adev->doorbell.ptr == NULL)
173 * amdgpu_doorbell_fini - Tear down doorbell driver information.
175 * @adev: amdgpu_device pointer
177 * Tear down doorbell driver information (CIK)
179 void amdgpu_doorbell_fini(struct amdgpu_device *adev)
181 iounmap(adev->doorbell.ptr);
182 adev->doorbell.ptr = NULL;