2 * Copyright 2015 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
24 #include <linux/list.h>
25 #include <linux/slab.h>
26 #include <linux/pci.h>
27 #include <linux/acpi.h>
29 #include <linux/firmware.h>
30 #include <drm/amdgpu_drm.h>
32 #include "cgs_linux.h"
34 #include "amdgpu_ucode.h"
36 struct amdgpu_cgs_device
{
37 struct cgs_device base
;
38 struct amdgpu_device
*adev
;
41 #define CGS_FUNC_ADEV \
42 struct amdgpu_device *adev = \
43 ((struct amdgpu_cgs_device *)cgs_device)->adev
45 static int amdgpu_cgs_gpu_mem_info(struct cgs_device
*cgs_device
, enum cgs_gpu_mem_type type
,
46 uint64_t *mc_start
, uint64_t *mc_size
,
51 case CGS_GPU_MEM_TYPE__VISIBLE_CONTIG_FB
:
52 case CGS_GPU_MEM_TYPE__VISIBLE_FB
:
54 *mc_size
= adev
->mc
.visible_vram_size
;
55 *mem_size
= adev
->mc
.visible_vram_size
- adev
->vram_pin_size
;
57 case CGS_GPU_MEM_TYPE__INVISIBLE_CONTIG_FB
:
58 case CGS_GPU_MEM_TYPE__INVISIBLE_FB
:
59 *mc_start
= adev
->mc
.visible_vram_size
;
60 *mc_size
= adev
->mc
.real_vram_size
- adev
->mc
.visible_vram_size
;
63 case CGS_GPU_MEM_TYPE__GART_CACHEABLE
:
64 case CGS_GPU_MEM_TYPE__GART_WRITECOMBINE
:
65 *mc_start
= adev
->mc
.gtt_start
;
66 *mc_size
= adev
->mc
.gtt_size
;
67 *mem_size
= adev
->mc
.gtt_size
- adev
->gart_pin_size
;
76 static int amdgpu_cgs_gmap_kmem(struct cgs_device
*cgs_device
, void *kmem
,
78 uint64_t min_offset
, uint64_t max_offset
,
79 cgs_handle_t
*kmem_handle
, uint64_t *mcaddr
)
84 struct page
*kmem_page
= vmalloc_to_page(kmem
);
85 int npages
= ALIGN(size
, PAGE_SIZE
) >> PAGE_SHIFT
;
87 struct sg_table
*sg
= drm_prime_pages_to_sg(&kmem_page
, npages
);
88 ret
= amdgpu_bo_create(adev
, size
, PAGE_SIZE
, false,
89 AMDGPU_GEM_DOMAIN_GTT
, 0, sg
, NULL
, &bo
);
92 ret
= amdgpu_bo_reserve(bo
, false);
93 if (unlikely(ret
!= 0))
96 /* pin buffer into GTT */
97 ret
= amdgpu_bo_pin_restricted(bo
, AMDGPU_GEM_DOMAIN_GTT
,
98 min_offset
, max_offset
, mcaddr
);
99 amdgpu_bo_unreserve(bo
);
101 *kmem_handle
= (cgs_handle_t
)bo
;
105 static int amdgpu_cgs_gunmap_kmem(struct cgs_device
*cgs_device
, cgs_handle_t kmem_handle
)
107 struct amdgpu_bo
*obj
= (struct amdgpu_bo
*)kmem_handle
;
110 int r
= amdgpu_bo_reserve(obj
, false);
111 if (likely(r
== 0)) {
112 amdgpu_bo_unpin(obj
);
113 amdgpu_bo_unreserve(obj
);
115 amdgpu_bo_unref(&obj
);
121 static int amdgpu_cgs_alloc_gpu_mem(struct cgs_device
*cgs_device
,
122 enum cgs_gpu_mem_type type
,
123 uint64_t size
, uint64_t align
,
124 uint64_t min_offset
, uint64_t max_offset
,
125 cgs_handle_t
*handle
)
131 struct amdgpu_bo
*obj
;
132 struct ttm_placement placement
;
133 struct ttm_place place
;
135 if (min_offset
> max_offset
) {
140 /* fail if the alignment is not a power of 2 */
141 if (((align
!= 1) && (align
& (align
- 1)))
142 || size
== 0 || align
== 0)
147 case CGS_GPU_MEM_TYPE__VISIBLE_CONTIG_FB
:
148 case CGS_GPU_MEM_TYPE__VISIBLE_FB
:
149 flags
= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED
|
150 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS
;
151 domain
= AMDGPU_GEM_DOMAIN_VRAM
;
152 if (max_offset
> adev
->mc
.real_vram_size
)
154 place
.fpfn
= min_offset
>> PAGE_SHIFT
;
155 place
.lpfn
= max_offset
>> PAGE_SHIFT
;
156 place
.flags
= TTM_PL_FLAG_WC
| TTM_PL_FLAG_UNCACHED
|
159 case CGS_GPU_MEM_TYPE__INVISIBLE_CONTIG_FB
:
160 case CGS_GPU_MEM_TYPE__INVISIBLE_FB
:
161 flags
= AMDGPU_GEM_CREATE_NO_CPU_ACCESS
|
162 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS
;
163 domain
= AMDGPU_GEM_DOMAIN_VRAM
;
164 if (adev
->mc
.visible_vram_size
< adev
->mc
.real_vram_size
) {
166 max(min_offset
, adev
->mc
.visible_vram_size
) >> PAGE_SHIFT
;
168 min(max_offset
, adev
->mc
.real_vram_size
) >> PAGE_SHIFT
;
169 place
.flags
= TTM_PL_FLAG_WC
| TTM_PL_FLAG_UNCACHED
|
174 case CGS_GPU_MEM_TYPE__GART_CACHEABLE
:
175 domain
= AMDGPU_GEM_DOMAIN_GTT
;
176 place
.fpfn
= min_offset
>> PAGE_SHIFT
;
177 place
.lpfn
= max_offset
>> PAGE_SHIFT
;
178 place
.flags
= TTM_PL_FLAG_CACHED
| TTM_PL_FLAG_TT
;
180 case CGS_GPU_MEM_TYPE__GART_WRITECOMBINE
:
181 flags
= AMDGPU_GEM_CREATE_CPU_GTT_USWC
;
182 domain
= AMDGPU_GEM_DOMAIN_GTT
;
183 place
.fpfn
= min_offset
>> PAGE_SHIFT
;
184 place
.lpfn
= max_offset
>> PAGE_SHIFT
;
185 place
.flags
= TTM_PL_FLAG_WC
| TTM_PL_FLAG_TT
|
186 TTM_PL_FLAG_UNCACHED
;
195 placement
.placement
= &place
;
196 placement
.num_placement
= 1;
197 placement
.busy_placement
= &place
;
198 placement
.num_busy_placement
= 1;
200 ret
= amdgpu_bo_create_restricted(adev
, size
, PAGE_SIZE
,
202 NULL
, &placement
, NULL
,
205 DRM_ERROR("(%d) bo create failed\n", ret
);
208 *handle
= (cgs_handle_t
)obj
;
213 static int amdgpu_cgs_free_gpu_mem(struct cgs_device
*cgs_device
, cgs_handle_t handle
)
215 struct amdgpu_bo
*obj
= (struct amdgpu_bo
*)handle
;
218 int r
= amdgpu_bo_reserve(obj
, false);
219 if (likely(r
== 0)) {
220 amdgpu_bo_kunmap(obj
);
221 amdgpu_bo_unpin(obj
);
222 amdgpu_bo_unreserve(obj
);
224 amdgpu_bo_unref(&obj
);
230 static int amdgpu_cgs_gmap_gpu_mem(struct cgs_device
*cgs_device
, cgs_handle_t handle
,
234 u64 min_offset
, max_offset
;
235 struct amdgpu_bo
*obj
= (struct amdgpu_bo
*)handle
;
237 WARN_ON_ONCE(obj
->placement
.num_placement
> 1);
239 min_offset
= obj
->placements
[0].fpfn
<< PAGE_SHIFT
;
240 max_offset
= obj
->placements
[0].lpfn
<< PAGE_SHIFT
;
242 r
= amdgpu_bo_reserve(obj
, false);
243 if (unlikely(r
!= 0))
245 r
= amdgpu_bo_pin_restricted(obj
, obj
->prefered_domains
,
246 min_offset
, max_offset
, mcaddr
);
247 amdgpu_bo_unreserve(obj
);
251 static int amdgpu_cgs_gunmap_gpu_mem(struct cgs_device
*cgs_device
, cgs_handle_t handle
)
254 struct amdgpu_bo
*obj
= (struct amdgpu_bo
*)handle
;
255 r
= amdgpu_bo_reserve(obj
, false);
256 if (unlikely(r
!= 0))
258 r
= amdgpu_bo_unpin(obj
);
259 amdgpu_bo_unreserve(obj
);
263 static int amdgpu_cgs_kmap_gpu_mem(struct cgs_device
*cgs_device
, cgs_handle_t handle
,
267 struct amdgpu_bo
*obj
= (struct amdgpu_bo
*)handle
;
268 r
= amdgpu_bo_reserve(obj
, false);
269 if (unlikely(r
!= 0))
271 r
= amdgpu_bo_kmap(obj
, map
);
272 amdgpu_bo_unreserve(obj
);
276 static int amdgpu_cgs_kunmap_gpu_mem(struct cgs_device
*cgs_device
, cgs_handle_t handle
)
279 struct amdgpu_bo
*obj
= (struct amdgpu_bo
*)handle
;
280 r
= amdgpu_bo_reserve(obj
, false);
281 if (unlikely(r
!= 0))
283 amdgpu_bo_kunmap(obj
);
284 amdgpu_bo_unreserve(obj
);
288 static uint32_t amdgpu_cgs_read_register(struct cgs_device
*cgs_device
, unsigned offset
)
291 return RREG32(offset
);
294 static void amdgpu_cgs_write_register(struct cgs_device
*cgs_device
, unsigned offset
,
298 WREG32(offset
, value
);
301 static uint32_t amdgpu_cgs_read_ind_register(struct cgs_device
*cgs_device
,
302 enum cgs_ind_reg space
,
307 case CGS_IND_REG__MMIO
:
308 return RREG32_IDX(index
);
309 case CGS_IND_REG__PCIE
:
310 return RREG32_PCIE(index
);
311 case CGS_IND_REG__SMC
:
312 return RREG32_SMC(index
);
313 case CGS_IND_REG__UVD_CTX
:
314 return RREG32_UVD_CTX(index
);
315 case CGS_IND_REG__DIDT
:
316 return RREG32_DIDT(index
);
317 case CGS_IND_REG_GC_CAC
:
318 return RREG32_GC_CAC(index
);
319 case CGS_IND_REG__AUDIO_ENDPT
:
320 DRM_ERROR("audio endpt register access not implemented.\n");
323 WARN(1, "Invalid indirect register space");
327 static void amdgpu_cgs_write_ind_register(struct cgs_device
*cgs_device
,
328 enum cgs_ind_reg space
,
329 unsigned index
, uint32_t value
)
333 case CGS_IND_REG__MMIO
:
334 return WREG32_IDX(index
, value
);
335 case CGS_IND_REG__PCIE
:
336 return WREG32_PCIE(index
, value
);
337 case CGS_IND_REG__SMC
:
338 return WREG32_SMC(index
, value
);
339 case CGS_IND_REG__UVD_CTX
:
340 return WREG32_UVD_CTX(index
, value
);
341 case CGS_IND_REG__DIDT
:
342 return WREG32_DIDT(index
, value
);
343 case CGS_IND_REG_GC_CAC
:
344 return WREG32_GC_CAC(index
, value
);
345 case CGS_IND_REG__AUDIO_ENDPT
:
346 DRM_ERROR("audio endpt register access not implemented.\n");
349 WARN(1, "Invalid indirect register space");
352 static uint8_t amdgpu_cgs_read_pci_config_byte(struct cgs_device
*cgs_device
, unsigned addr
)
356 int ret
= pci_read_config_byte(adev
->pdev
, addr
, &val
);
357 if (WARN(ret
, "pci_read_config_byte error"))
362 static uint16_t amdgpu_cgs_read_pci_config_word(struct cgs_device
*cgs_device
, unsigned addr
)
366 int ret
= pci_read_config_word(adev
->pdev
, addr
, &val
);
367 if (WARN(ret
, "pci_read_config_word error"))
372 static uint32_t amdgpu_cgs_read_pci_config_dword(struct cgs_device
*cgs_device
,
377 int ret
= pci_read_config_dword(adev
->pdev
, addr
, &val
);
378 if (WARN(ret
, "pci_read_config_dword error"))
383 static void amdgpu_cgs_write_pci_config_byte(struct cgs_device
*cgs_device
, unsigned addr
,
387 int ret
= pci_write_config_byte(adev
->pdev
, addr
, value
);
388 WARN(ret
, "pci_write_config_byte error");
391 static void amdgpu_cgs_write_pci_config_word(struct cgs_device
*cgs_device
, unsigned addr
,
395 int ret
= pci_write_config_word(adev
->pdev
, addr
, value
);
396 WARN(ret
, "pci_write_config_word error");
399 static void amdgpu_cgs_write_pci_config_dword(struct cgs_device
*cgs_device
, unsigned addr
,
403 int ret
= pci_write_config_dword(adev
->pdev
, addr
, value
);
404 WARN(ret
, "pci_write_config_dword error");
408 static int amdgpu_cgs_get_pci_resource(struct cgs_device
*cgs_device
,
409 enum cgs_resource_type resource_type
,
412 uint64_t *resource_base
)
416 if (resource_base
== NULL
)
419 switch (resource_type
) {
420 case CGS_RESOURCE_TYPE_MMIO
:
421 if (adev
->rmmio_size
== 0)
423 if ((offset
+ size
) > adev
->rmmio_size
)
425 *resource_base
= adev
->rmmio_base
;
427 case CGS_RESOURCE_TYPE_DOORBELL
:
428 if (adev
->doorbell
.size
== 0)
430 if ((offset
+ size
) > adev
->doorbell
.size
)
432 *resource_base
= adev
->doorbell
.base
;
434 case CGS_RESOURCE_TYPE_FB
:
435 case CGS_RESOURCE_TYPE_IO
:
436 case CGS_RESOURCE_TYPE_ROM
:
442 static const void *amdgpu_cgs_atom_get_data_table(struct cgs_device
*cgs_device
,
443 unsigned table
, uint16_t *size
,
444 uint8_t *frev
, uint8_t *crev
)
449 if (amdgpu_atom_parse_data_header(
450 adev
->mode_info
.atom_context
, table
, size
,
451 frev
, crev
, &data_start
))
452 return (uint8_t*)adev
->mode_info
.atom_context
->bios
+
458 static int amdgpu_cgs_atom_get_cmd_table_revs(struct cgs_device
*cgs_device
, unsigned table
,
459 uint8_t *frev
, uint8_t *crev
)
463 if (amdgpu_atom_parse_cmd_header(
464 adev
->mode_info
.atom_context
, table
,
471 static int amdgpu_cgs_atom_exec_cmd_table(struct cgs_device
*cgs_device
, unsigned table
,
476 return amdgpu_atom_execute_table(
477 adev
->mode_info
.atom_context
, table
, args
);
480 static int amdgpu_cgs_create_pm_request(struct cgs_device
*cgs_device
, cgs_handle_t
*request
)
486 static int amdgpu_cgs_destroy_pm_request(struct cgs_device
*cgs_device
, cgs_handle_t request
)
492 static int amdgpu_cgs_set_pm_request(struct cgs_device
*cgs_device
, cgs_handle_t request
,
499 static int amdgpu_cgs_pm_request_clock(struct cgs_device
*cgs_device
, cgs_handle_t request
,
500 enum cgs_clock clock
, unsigned freq
)
506 static int amdgpu_cgs_pm_request_engine(struct cgs_device
*cgs_device
, cgs_handle_t request
,
507 enum cgs_engine engine
, int powered
)
515 static int amdgpu_cgs_pm_query_clock_limits(struct cgs_device
*cgs_device
,
516 enum cgs_clock clock
,
517 struct cgs_clock_limits
*limits
)
523 static int amdgpu_cgs_set_camera_voltages(struct cgs_device
*cgs_device
, uint32_t mask
,
524 const uint32_t *voltages
)
526 DRM_ERROR("not implemented");
530 struct cgs_irq_params
{
532 cgs_irq_source_set_func_t set
;
533 cgs_irq_handler_func_t handler
;
537 static int cgs_set_irq_state(struct amdgpu_device
*adev
,
538 struct amdgpu_irq_src
*src
,
540 enum amdgpu_interrupt_state state
)
542 struct cgs_irq_params
*irq_params
=
543 (struct cgs_irq_params
*)src
->data
;
546 if (!irq_params
->set
)
548 return irq_params
->set(irq_params
->private_data
,
554 static int cgs_process_irq(struct amdgpu_device
*adev
,
555 struct amdgpu_irq_src
*source
,
556 struct amdgpu_iv_entry
*entry
)
558 struct cgs_irq_params
*irq_params
=
559 (struct cgs_irq_params
*)source
->data
;
562 if (!irq_params
->handler
)
564 return irq_params
->handler(irq_params
->private_data
,
569 static const struct amdgpu_irq_src_funcs cgs_irq_funcs
= {
570 .set
= cgs_set_irq_state
,
571 .process
= cgs_process_irq
,
574 static int amdgpu_cgs_add_irq_source(void *cgs_device
,
578 cgs_irq_source_set_func_t set
,
579 cgs_irq_handler_func_t handler
,
584 struct cgs_irq_params
*irq_params
;
585 struct amdgpu_irq_src
*source
=
586 kzalloc(sizeof(struct amdgpu_irq_src
), GFP_KERNEL
);
590 kzalloc(sizeof(struct cgs_irq_params
), GFP_KERNEL
);
595 source
->num_types
= num_types
;
596 source
->funcs
= &cgs_irq_funcs
;
597 irq_params
->src_id
= src_id
;
598 irq_params
->set
= set
;
599 irq_params
->handler
= handler
;
600 irq_params
->private_data
= private_data
;
601 source
->data
= (void *)irq_params
;
602 ret
= amdgpu_irq_add_id(adev
, client_id
, src_id
, source
);
611 static int amdgpu_cgs_irq_get(void *cgs_device
, unsigned client_id
,
612 unsigned src_id
, unsigned type
)
616 if (!adev
->irq
.client
[client_id
].sources
)
619 return amdgpu_irq_get(adev
, adev
->irq
.client
[client_id
].sources
[src_id
], type
);
622 static int amdgpu_cgs_irq_put(void *cgs_device
, unsigned client_id
,
623 unsigned src_id
, unsigned type
)
627 if (!adev
->irq
.client
[client_id
].sources
)
630 return amdgpu_irq_put(adev
, adev
->irq
.client
[client_id
].sources
[src_id
], type
);
633 static int amdgpu_cgs_set_clockgating_state(struct cgs_device
*cgs_device
,
634 enum amd_ip_block_type block_type
,
635 enum amd_clockgating_state state
)
640 for (i
= 0; i
< adev
->num_ip_blocks
; i
++) {
641 if (!adev
->ip_blocks
[i
].status
.valid
)
644 if (adev
->ip_blocks
[i
].version
->type
== block_type
) {
645 r
= adev
->ip_blocks
[i
].version
->funcs
->set_clockgating_state(
654 static int amdgpu_cgs_set_powergating_state(struct cgs_device
*cgs_device
,
655 enum amd_ip_block_type block_type
,
656 enum amd_powergating_state state
)
661 for (i
= 0; i
< adev
->num_ip_blocks
; i
++) {
662 if (!adev
->ip_blocks
[i
].status
.valid
)
665 if (adev
->ip_blocks
[i
].version
->type
== block_type
) {
666 r
= adev
->ip_blocks
[i
].version
->funcs
->set_powergating_state(
676 static uint32_t fw_type_convert(struct cgs_device
*cgs_device
, uint32_t fw_type
)
679 enum AMDGPU_UCODE_ID result
= AMDGPU_UCODE_ID_MAXIMUM
;
682 case CGS_UCODE_ID_SDMA0
:
683 result
= AMDGPU_UCODE_ID_SDMA0
;
685 case CGS_UCODE_ID_SDMA1
:
686 result
= AMDGPU_UCODE_ID_SDMA1
;
688 case CGS_UCODE_ID_CP_CE
:
689 result
= AMDGPU_UCODE_ID_CP_CE
;
691 case CGS_UCODE_ID_CP_PFP
:
692 result
= AMDGPU_UCODE_ID_CP_PFP
;
694 case CGS_UCODE_ID_CP_ME
:
695 result
= AMDGPU_UCODE_ID_CP_ME
;
697 case CGS_UCODE_ID_CP_MEC
:
698 case CGS_UCODE_ID_CP_MEC_JT1
:
699 result
= AMDGPU_UCODE_ID_CP_MEC1
;
701 case CGS_UCODE_ID_CP_MEC_JT2
:
702 /* for VI. JT2 should be the same as JT1, because:
703 1, MEC2 and MEC1 use exactly same FW.
704 2, JT2 is not pached but JT1 is.
706 if (adev
->asic_type
>= CHIP_TOPAZ
)
707 result
= AMDGPU_UCODE_ID_CP_MEC1
;
709 result
= AMDGPU_UCODE_ID_CP_MEC2
;
711 case CGS_UCODE_ID_RLC_G
:
712 result
= AMDGPU_UCODE_ID_RLC_G
;
714 case CGS_UCODE_ID_STORAGE
:
715 result
= AMDGPU_UCODE_ID_STORAGE
;
718 DRM_ERROR("Firmware type not supported\n");
723 static int amdgpu_cgs_rel_firmware(struct cgs_device
*cgs_device
, enum cgs_ucode_id type
)
726 if ((CGS_UCODE_ID_SMU
== type
) || (CGS_UCODE_ID_SMU_SK
== type
)) {
727 release_firmware(adev
->pm
.fw
);
731 /* cannot release other firmware because they are not created by cgs */
735 static uint16_t amdgpu_get_firmware_version(struct cgs_device
*cgs_device
,
736 enum cgs_ucode_id type
)
739 uint16_t fw_version
= 0;
742 case CGS_UCODE_ID_SDMA0
:
743 fw_version
= adev
->sdma
.instance
[0].fw_version
;
745 case CGS_UCODE_ID_SDMA1
:
746 fw_version
= adev
->sdma
.instance
[1].fw_version
;
748 case CGS_UCODE_ID_CP_CE
:
749 fw_version
= adev
->gfx
.ce_fw_version
;
751 case CGS_UCODE_ID_CP_PFP
:
752 fw_version
= adev
->gfx
.pfp_fw_version
;
754 case CGS_UCODE_ID_CP_ME
:
755 fw_version
= adev
->gfx
.me_fw_version
;
757 case CGS_UCODE_ID_CP_MEC
:
758 fw_version
= adev
->gfx
.mec_fw_version
;
760 case CGS_UCODE_ID_CP_MEC_JT1
:
761 fw_version
= adev
->gfx
.mec_fw_version
;
763 case CGS_UCODE_ID_CP_MEC_JT2
:
764 fw_version
= adev
->gfx
.mec_fw_version
;
766 case CGS_UCODE_ID_RLC_G
:
767 fw_version
= adev
->gfx
.rlc_fw_version
;
769 case CGS_UCODE_ID_STORAGE
:
772 DRM_ERROR("firmware type %d do not have version\n", type
);
778 static int amdgpu_cgs_enter_safe_mode(struct cgs_device
*cgs_device
,
783 if (adev
->gfx
.rlc
.funcs
->enter_safe_mode
== NULL
||
784 adev
->gfx
.rlc
.funcs
->exit_safe_mode
== NULL
)
788 adev
->gfx
.rlc
.funcs
->enter_safe_mode(adev
);
790 adev
->gfx
.rlc
.funcs
->exit_safe_mode(adev
);
795 static int amdgpu_cgs_get_firmware_info(struct cgs_device
*cgs_device
,
796 enum cgs_ucode_id type
,
797 struct cgs_firmware_info
*info
)
801 if ((CGS_UCODE_ID_SMU
!= type
) && (CGS_UCODE_ID_SMU_SK
!= type
)) {
804 const struct gfx_firmware_header_v1_0
*header
;
805 enum AMDGPU_UCODE_ID id
;
806 struct amdgpu_firmware_info
*ucode
;
808 id
= fw_type_convert(cgs_device
, type
);
809 ucode
= &adev
->firmware
.ucode
[id
];
810 if (ucode
->fw
== NULL
)
813 gpu_addr
= ucode
->mc_addr
;
814 header
= (const struct gfx_firmware_header_v1_0
*)ucode
->fw
->data
;
815 data_size
= le32_to_cpu(header
->header
.ucode_size_bytes
);
817 if ((type
== CGS_UCODE_ID_CP_MEC_JT1
) ||
818 (type
== CGS_UCODE_ID_CP_MEC_JT2
)) {
819 gpu_addr
+= ALIGN(le32_to_cpu(header
->header
.ucode_size_bytes
), PAGE_SIZE
);
820 data_size
= le32_to_cpu(header
->jt_size
) << 2;
823 info
->kptr
= ucode
->kaddr
;
824 info
->image_size
= data_size
;
825 info
->mc_addr
= gpu_addr
;
826 info
->version
= (uint16_t)le32_to_cpu(header
->header
.ucode_version
);
828 if (CGS_UCODE_ID_CP_MEC
== type
)
829 info
->image_size
= (header
->jt_offset
) << 2;
831 info
->fw_version
= amdgpu_get_firmware_version(cgs_device
, type
);
832 info
->feature_version
= (uint16_t)le32_to_cpu(header
->ucode_feature_version
);
834 char fw_name
[30] = {0};
837 uint32_t ucode_start_address
;
839 const struct smc_firmware_header_v1_0
*hdr
;
840 const struct common_firmware_header
*header
;
841 struct amdgpu_firmware_info
*ucode
= NULL
;
844 switch (adev
->asic_type
) {
846 if (((adev
->pdev
->device
== 0x6900) && (adev
->pdev
->revision
== 0x81)) ||
847 ((adev
->pdev
->device
== 0x6900) && (adev
->pdev
->revision
== 0x83)) ||
848 ((adev
->pdev
->device
== 0x6907) && (adev
->pdev
->revision
== 0x87))) {
849 info
->is_kicker
= true;
850 strcpy(fw_name
, "amdgpu/topaz_k_smc.bin");
852 strcpy(fw_name
, "amdgpu/topaz_smc.bin");
855 if (((adev
->pdev
->device
== 0x6939) && (adev
->pdev
->revision
== 0xf1)) ||
856 ((adev
->pdev
->device
== 0x6938) && (adev
->pdev
->revision
== 0xf1))) {
857 info
->is_kicker
= true;
858 strcpy(fw_name
, "amdgpu/tonga_k_smc.bin");
860 strcpy(fw_name
, "amdgpu/tonga_smc.bin");
863 strcpy(fw_name
, "amdgpu/fiji_smc.bin");
866 if (type
== CGS_UCODE_ID_SMU
) {
867 if (((adev
->pdev
->device
== 0x67ef) &&
868 ((adev
->pdev
->revision
== 0xe0) ||
869 (adev
->pdev
->revision
== 0xe2) ||
870 (adev
->pdev
->revision
== 0xe5))) ||
871 ((adev
->pdev
->device
== 0x67ff) &&
872 ((adev
->pdev
->revision
== 0xcf) ||
873 (adev
->pdev
->revision
== 0xef) ||
874 (adev
->pdev
->revision
== 0xff)))) {
875 info
->is_kicker
= true;
876 strcpy(fw_name
, "amdgpu/polaris11_k_smc.bin");
878 strcpy(fw_name
, "amdgpu/polaris11_smc.bin");
879 } else if (type
== CGS_UCODE_ID_SMU_SK
) {
880 strcpy(fw_name
, "amdgpu/polaris11_smc_sk.bin");
884 if (type
== CGS_UCODE_ID_SMU
) {
885 if ((adev
->pdev
->device
== 0x67df) &&
886 ((adev
->pdev
->revision
== 0xe0) ||
887 (adev
->pdev
->revision
== 0xe3) ||
888 (adev
->pdev
->revision
== 0xe4) ||
889 (adev
->pdev
->revision
== 0xe5) ||
890 (adev
->pdev
->revision
== 0xe7) ||
891 (adev
->pdev
->revision
== 0xef))) {
892 info
->is_kicker
= true;
893 strcpy(fw_name
, "amdgpu/polaris10_k_smc.bin");
895 strcpy(fw_name
, "amdgpu/polaris10_smc.bin");
896 } else if (type
== CGS_UCODE_ID_SMU_SK
) {
897 strcpy(fw_name
, "amdgpu/polaris10_smc_sk.bin");
901 strcpy(fw_name
, "amdgpu/polaris12_smc.bin");
904 strcpy(fw_name
, "amdgpu/vega10_smc.bin");
907 DRM_ERROR("SMC firmware not supported\n");
911 err
= request_firmware(&adev
->pm
.fw
, fw_name
, adev
->dev
);
913 DRM_ERROR("Failed to request firmware\n");
917 err
= amdgpu_ucode_validate(adev
->pm
.fw
);
919 DRM_ERROR("Failed to load firmware \"%s\"", fw_name
);
920 release_firmware(adev
->pm
.fw
);
925 if (adev
->firmware
.load_type
== AMDGPU_FW_LOAD_PSP
) {
926 ucode
= &adev
->firmware
.ucode
[AMDGPU_UCODE_ID_SMC
];
927 ucode
->ucode_id
= AMDGPU_UCODE_ID_SMC
;
928 ucode
->fw
= adev
->pm
.fw
;
929 header
= (const struct common_firmware_header
*)ucode
->fw
->data
;
930 adev
->firmware
.fw_size
+=
931 ALIGN(le32_to_cpu(header
->ucode_size_bytes
), PAGE_SIZE
);
935 hdr
= (const struct smc_firmware_header_v1_0
*) adev
->pm
.fw
->data
;
936 amdgpu_ucode_print_smc_hdr(&hdr
->header
);
937 adev
->pm
.fw_version
= le32_to_cpu(hdr
->header
.ucode_version
);
938 ucode_size
= le32_to_cpu(hdr
->header
.ucode_size_bytes
);
939 ucode_start_address
= le32_to_cpu(hdr
->ucode_start_addr
);
940 src
= (const uint8_t *)(adev
->pm
.fw
->data
+
941 le32_to_cpu(hdr
->header
.ucode_array_offset_bytes
));
943 info
->version
= adev
->pm
.fw_version
;
944 info
->image_size
= ucode_size
;
945 info
->ucode_start_address
= ucode_start_address
;
946 info
->kptr
= (void *)src
;
951 static int amdgpu_cgs_is_virtualization_enabled(void *cgs_device
)
954 return amdgpu_sriov_vf(adev
);
957 static int amdgpu_cgs_query_system_info(struct cgs_device
*cgs_device
,
958 struct cgs_system_info
*sys_info
)
962 if (NULL
== sys_info
)
965 if (sizeof(struct cgs_system_info
) != sys_info
->size
)
968 switch (sys_info
->info_id
) {
969 case CGS_SYSTEM_INFO_ADAPTER_BDF_ID
:
970 sys_info
->value
= adev
->pdev
->devfn
| (adev
->pdev
->bus
->number
<< 8);
972 case CGS_SYSTEM_INFO_PCIE_GEN_INFO
:
973 sys_info
->value
= adev
->pm
.pcie_gen_mask
;
975 case CGS_SYSTEM_INFO_PCIE_MLW
:
976 sys_info
->value
= adev
->pm
.pcie_mlw_mask
;
978 case CGS_SYSTEM_INFO_PCIE_DEV
:
979 sys_info
->value
= adev
->pdev
->device
;
981 case CGS_SYSTEM_INFO_PCIE_REV
:
982 sys_info
->value
= adev
->pdev
->revision
;
984 case CGS_SYSTEM_INFO_CG_FLAGS
:
985 sys_info
->value
= adev
->cg_flags
;
987 case CGS_SYSTEM_INFO_PG_FLAGS
:
988 sys_info
->value
= adev
->pg_flags
;
990 case CGS_SYSTEM_INFO_GFX_CU_INFO
:
991 sys_info
->value
= adev
->gfx
.cu_info
.number
;
993 case CGS_SYSTEM_INFO_GFX_SE_INFO
:
994 sys_info
->value
= adev
->gfx
.config
.max_shader_engines
;
996 case CGS_SYSTEM_INFO_PCIE_SUB_SYS_ID
:
997 sys_info
->value
= adev
->pdev
->subsystem_device
;
999 case CGS_SYSTEM_INFO_PCIE_SUB_SYS_VENDOR_ID
:
1000 sys_info
->value
= adev
->pdev
->subsystem_vendor
;
1009 static int amdgpu_cgs_get_active_displays_info(struct cgs_device
*cgs_device
,
1010 struct cgs_display_info
*info
)
1013 struct amdgpu_crtc
*amdgpu_crtc
;
1014 struct drm_device
*ddev
= adev
->ddev
;
1015 struct drm_crtc
*crtc
;
1016 uint32_t line_time_us
, vblank_lines
;
1017 struct cgs_mode_info
*mode_info
;
1022 mode_info
= info
->mode_info
;
1024 if (adev
->mode_info
.num_crtc
&& adev
->mode_info
.mode_config_initialized
) {
1025 list_for_each_entry(crtc
,
1026 &ddev
->mode_config
.crtc_list
, head
) {
1027 amdgpu_crtc
= to_amdgpu_crtc(crtc
);
1028 if (crtc
->enabled
) {
1029 info
->active_display_mask
|= (1 << amdgpu_crtc
->crtc_id
);
1030 info
->display_count
++;
1032 if (mode_info
!= NULL
&&
1033 crtc
->enabled
&& amdgpu_crtc
->enabled
&&
1034 amdgpu_crtc
->hw_mode
.clock
) {
1035 line_time_us
= (amdgpu_crtc
->hw_mode
.crtc_htotal
* 1000) /
1036 amdgpu_crtc
->hw_mode
.clock
;
1037 vblank_lines
= amdgpu_crtc
->hw_mode
.crtc_vblank_end
-
1038 amdgpu_crtc
->hw_mode
.crtc_vdisplay
+
1039 (amdgpu_crtc
->v_border
* 2);
1040 mode_info
->vblank_time_us
= vblank_lines
* line_time_us
;
1041 mode_info
->refresh_rate
= drm_mode_vrefresh(&amdgpu_crtc
->hw_mode
);
1042 mode_info
->ref_clock
= adev
->clock
.spll
.reference_freq
;
1052 static int amdgpu_cgs_notify_dpm_enabled(struct cgs_device
*cgs_device
, bool enabled
)
1056 adev
->pm
.dpm_enabled
= enabled
;
1061 /** \brief evaluate acpi namespace object, handle or pathname must be valid
1063 * \param info input/output arguments for the control method
1067 #if defined(CONFIG_ACPI)
1068 static int amdgpu_cgs_acpi_eval_object(struct cgs_device
*cgs_device
,
1069 struct cgs_acpi_method_info
*info
)
1073 struct acpi_object_list input
;
1074 struct acpi_buffer output
= { ACPI_ALLOCATE_BUFFER
, NULL
};
1075 union acpi_object
*params
, *obj
;
1076 uint8_t name
[5] = {'\0'};
1077 struct cgs_acpi_method_argument
*argument
;
1082 handle
= ACPI_HANDLE(&adev
->pdev
->dev
);
1086 memset(&input
, 0, sizeof(struct acpi_object_list
));
1088 /* validate input info */
1089 if (info
->size
!= sizeof(struct cgs_acpi_method_info
))
1092 input
.count
= info
->input_count
;
1093 if (info
->input_count
> 0) {
1094 if (info
->pinput_argument
== NULL
)
1096 argument
= info
->pinput_argument
;
1097 for (i
= 0; i
< info
->input_count
; i
++) {
1098 if (((argument
->type
== ACPI_TYPE_STRING
) ||
1099 (argument
->type
== ACPI_TYPE_BUFFER
)) &&
1100 (argument
->pointer
== NULL
))
1106 if (info
->output_count
> 0) {
1107 if (info
->poutput_argument
== NULL
)
1109 argument
= info
->poutput_argument
;
1110 for (i
= 0; i
< info
->output_count
; i
++) {
1111 if (((argument
->type
== ACPI_TYPE_STRING
) ||
1112 (argument
->type
== ACPI_TYPE_BUFFER
))
1113 && (argument
->pointer
== NULL
))
1119 /* The path name passed to acpi_evaluate_object should be null terminated */
1120 if ((info
->field
& CGS_ACPI_FIELD_METHOD_NAME
) != 0) {
1121 strncpy(name
, (char *)&(info
->name
), sizeof(uint32_t));
1125 /* parse input parameters */
1126 if (input
.count
> 0) {
1127 input
.pointer
= params
=
1128 kzalloc(sizeof(union acpi_object
) * input
.count
, GFP_KERNEL
);
1132 argument
= info
->pinput_argument
;
1134 for (i
= 0; i
< input
.count
; i
++) {
1135 params
->type
= argument
->type
;
1136 switch (params
->type
) {
1137 case ACPI_TYPE_INTEGER
:
1138 params
->integer
.value
= argument
->value
;
1140 case ACPI_TYPE_STRING
:
1141 params
->string
.length
= argument
->data_length
;
1142 params
->string
.pointer
= argument
->pointer
;
1144 case ACPI_TYPE_BUFFER
:
1145 params
->buffer
.length
= argument
->data_length
;
1146 params
->buffer
.pointer
= argument
->pointer
;
1156 /* parse output info */
1157 count
= info
->output_count
;
1158 argument
= info
->poutput_argument
;
1160 /* evaluate the acpi method */
1161 status
= acpi_evaluate_object(handle
, name
, &input
, &output
);
1163 if (ACPI_FAILURE(status
)) {
1168 /* return the output info */
1169 obj
= output
.pointer
;
1172 if ((obj
->type
!= ACPI_TYPE_PACKAGE
) ||
1173 (obj
->package
.count
!= count
)) {
1177 params
= obj
->package
.elements
;
1181 if (params
== NULL
) {
1186 for (i
= 0; i
< count
; i
++) {
1187 if (argument
->type
!= params
->type
) {
1191 switch (params
->type
) {
1192 case ACPI_TYPE_INTEGER
:
1193 argument
->value
= params
->integer
.value
;
1195 case ACPI_TYPE_STRING
:
1196 if ((params
->string
.length
!= argument
->data_length
) ||
1197 (params
->string
.pointer
== NULL
)) {
1201 strncpy(argument
->pointer
,
1202 params
->string
.pointer
,
1203 params
->string
.length
);
1205 case ACPI_TYPE_BUFFER
:
1206 if (params
->buffer
.pointer
== NULL
) {
1210 memcpy(argument
->pointer
,
1211 params
->buffer
.pointer
,
1212 argument
->data_length
);
1225 kfree((void *)input
.pointer
);
1229 static int amdgpu_cgs_acpi_eval_object(struct cgs_device
*cgs_device
,
1230 struct cgs_acpi_method_info
*info
)
1236 static int amdgpu_cgs_call_acpi_method(struct cgs_device
*cgs_device
,
1237 uint32_t acpi_method
,
1238 uint32_t acpi_function
,
1239 void *pinput
, void *poutput
,
1240 uint32_t output_count
,
1241 uint32_t input_size
,
1242 uint32_t output_size
)
1244 struct cgs_acpi_method_argument acpi_input
[2] = { {0}, {0} };
1245 struct cgs_acpi_method_argument acpi_output
= {0};
1246 struct cgs_acpi_method_info info
= {0};
1248 acpi_input
[0].type
= CGS_ACPI_TYPE_INTEGER
;
1249 acpi_input
[0].data_length
= sizeof(uint32_t);
1250 acpi_input
[0].value
= acpi_function
;
1252 acpi_input
[1].type
= CGS_ACPI_TYPE_BUFFER
;
1253 acpi_input
[1].data_length
= input_size
;
1254 acpi_input
[1].pointer
= pinput
;
1256 acpi_output
.type
= CGS_ACPI_TYPE_BUFFER
;
1257 acpi_output
.data_length
= output_size
;
1258 acpi_output
.pointer
= poutput
;
1260 info
.size
= sizeof(struct cgs_acpi_method_info
);
1261 info
.field
= CGS_ACPI_FIELD_METHOD_NAME
| CGS_ACPI_FIELD_INPUT_ARGUMENT_COUNT
;
1262 info
.input_count
= 2;
1263 info
.name
= acpi_method
;
1264 info
.pinput_argument
= acpi_input
;
1265 info
.output_count
= output_count
;
1266 info
.poutput_argument
= &acpi_output
;
1268 return amdgpu_cgs_acpi_eval_object(cgs_device
, &info
);
1271 static const struct cgs_ops amdgpu_cgs_ops
= {
1272 .gpu_mem_info
= amdgpu_cgs_gpu_mem_info
,
1273 .gmap_kmem
= amdgpu_cgs_gmap_kmem
,
1274 .gunmap_kmem
= amdgpu_cgs_gunmap_kmem
,
1275 .alloc_gpu_mem
= amdgpu_cgs_alloc_gpu_mem
,
1276 .free_gpu_mem
= amdgpu_cgs_free_gpu_mem
,
1277 .gmap_gpu_mem
= amdgpu_cgs_gmap_gpu_mem
,
1278 .gunmap_gpu_mem
= amdgpu_cgs_gunmap_gpu_mem
,
1279 .kmap_gpu_mem
= amdgpu_cgs_kmap_gpu_mem
,
1280 .kunmap_gpu_mem
= amdgpu_cgs_kunmap_gpu_mem
,
1281 .read_register
= amdgpu_cgs_read_register
,
1282 .write_register
= amdgpu_cgs_write_register
,
1283 .read_ind_register
= amdgpu_cgs_read_ind_register
,
1284 .write_ind_register
= amdgpu_cgs_write_ind_register
,
1285 .read_pci_config_byte
= amdgpu_cgs_read_pci_config_byte
,
1286 .read_pci_config_word
= amdgpu_cgs_read_pci_config_word
,
1287 .read_pci_config_dword
= amdgpu_cgs_read_pci_config_dword
,
1288 .write_pci_config_byte
= amdgpu_cgs_write_pci_config_byte
,
1289 .write_pci_config_word
= amdgpu_cgs_write_pci_config_word
,
1290 .write_pci_config_dword
= amdgpu_cgs_write_pci_config_dword
,
1291 .get_pci_resource
= amdgpu_cgs_get_pci_resource
,
1292 .atom_get_data_table
= amdgpu_cgs_atom_get_data_table
,
1293 .atom_get_cmd_table_revs
= amdgpu_cgs_atom_get_cmd_table_revs
,
1294 .atom_exec_cmd_table
= amdgpu_cgs_atom_exec_cmd_table
,
1295 .create_pm_request
= amdgpu_cgs_create_pm_request
,
1296 .destroy_pm_request
= amdgpu_cgs_destroy_pm_request
,
1297 .set_pm_request
= amdgpu_cgs_set_pm_request
,
1298 .pm_request_clock
= amdgpu_cgs_pm_request_clock
,
1299 .pm_request_engine
= amdgpu_cgs_pm_request_engine
,
1300 .pm_query_clock_limits
= amdgpu_cgs_pm_query_clock_limits
,
1301 .set_camera_voltages
= amdgpu_cgs_set_camera_voltages
,
1302 .get_firmware_info
= amdgpu_cgs_get_firmware_info
,
1303 .rel_firmware
= amdgpu_cgs_rel_firmware
,
1304 .set_powergating_state
= amdgpu_cgs_set_powergating_state
,
1305 .set_clockgating_state
= amdgpu_cgs_set_clockgating_state
,
1306 .get_active_displays_info
= amdgpu_cgs_get_active_displays_info
,
1307 .notify_dpm_enabled
= amdgpu_cgs_notify_dpm_enabled
,
1308 .call_acpi_method
= amdgpu_cgs_call_acpi_method
,
1309 .query_system_info
= amdgpu_cgs_query_system_info
,
1310 .is_virtualization_enabled
= amdgpu_cgs_is_virtualization_enabled
,
1311 .enter_safe_mode
= amdgpu_cgs_enter_safe_mode
,
1314 static const struct cgs_os_ops amdgpu_cgs_os_ops
= {
1315 .add_irq_source
= amdgpu_cgs_add_irq_source
,
1316 .irq_get
= amdgpu_cgs_irq_get
,
1317 .irq_put
= amdgpu_cgs_irq_put
1320 struct cgs_device
*amdgpu_cgs_create_device(struct amdgpu_device
*adev
)
1322 struct amdgpu_cgs_device
*cgs_device
=
1323 kmalloc(sizeof(*cgs_device
), GFP_KERNEL
);
1326 DRM_ERROR("Couldn't allocate CGS device structure\n");
1330 cgs_device
->base
.ops
= &amdgpu_cgs_ops
;
1331 cgs_device
->base
.os_ops
= &amdgpu_cgs_os_ops
;
1332 cgs_device
->adev
= adev
;
1334 return (struct cgs_device
*)cgs_device
;
1337 void amdgpu_cgs_destroy_device(struct cgs_device
*cgs_device
)