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/pci.h>
26 #include "cgs_linux.h"
29 struct amdgpu_cgs_device
{
30 struct cgs_device base
;
31 struct amdgpu_device
*adev
;
34 #define CGS_FUNC_ADEV \
35 struct amdgpu_device *adev = \
36 ((struct amdgpu_cgs_device *)cgs_device)->adev
38 static int amdgpu_cgs_gpu_mem_info(void *cgs_device
, enum cgs_gpu_mem_type type
,
39 uint64_t *mc_start
, uint64_t *mc_size
,
45 static int amdgpu_cgs_gmap_kmem(void *cgs_device
, void *kmem
,
47 uint64_t min_offset
, uint64_t max_offset
,
48 cgs_handle_t
*kmem_handle
, uint64_t *mcaddr
)
53 static int amdgpu_cgs_gunmap_kmem(void *cgs_device
, cgs_handle_t kmem_handle
)
58 static int amdgpu_cgs_alloc_gpu_mem(void *cgs_device
,
59 enum cgs_gpu_mem_type type
,
60 uint64_t size
, uint64_t align
,
61 uint64_t min_offset
, uint64_t max_offset
,
67 static int amdgpu_cgs_import_gpu_mem(void *cgs_device
, int dmabuf_fd
,
74 static int amdgpu_cgs_free_gpu_mem(void *cgs_device
, cgs_handle_t handle
)
80 static int amdgpu_cgs_gmap_gpu_mem(void *cgs_device
, cgs_handle_t handle
,
87 static int amdgpu_cgs_gunmap_gpu_mem(void *cgs_device
, cgs_handle_t handle
)
93 static int amdgpu_cgs_kmap_gpu_mem(void *cgs_device
, cgs_handle_t handle
,
100 static int amdgpu_cgs_kunmap_gpu_mem(void *cgs_device
, cgs_handle_t handle
)
106 static uint32_t amdgpu_cgs_read_register(void *cgs_device
, unsigned offset
)
109 return RREG32(offset
);
112 static void amdgpu_cgs_write_register(void *cgs_device
, unsigned offset
,
116 WREG32(offset
, value
);
119 static uint32_t amdgpu_cgs_read_ind_register(void *cgs_device
,
120 enum cgs_ind_reg space
,
125 case CGS_IND_REG__MMIO
:
126 return RREG32_IDX(index
);
127 case CGS_IND_REG__PCIE
:
128 return RREG32_PCIE(index
);
129 case CGS_IND_REG__SMC
:
130 return RREG32_SMC(index
);
131 case CGS_IND_REG__UVD_CTX
:
132 return RREG32_UVD_CTX(index
);
133 case CGS_IND_REG__DIDT
:
134 return RREG32_DIDT(index
);
135 case CGS_IND_REG__AUDIO_ENDPT
:
136 DRM_ERROR("audio endpt register access not implemented.\n");
139 WARN(1, "Invalid indirect register space");
143 static void amdgpu_cgs_write_ind_register(void *cgs_device
,
144 enum cgs_ind_reg space
,
145 unsigned index
, uint32_t value
)
149 case CGS_IND_REG__MMIO
:
150 return WREG32_IDX(index
, value
);
151 case CGS_IND_REG__PCIE
:
152 return WREG32_PCIE(index
, value
);
153 case CGS_IND_REG__SMC
:
154 return WREG32_SMC(index
, value
);
155 case CGS_IND_REG__UVD_CTX
:
156 return WREG32_UVD_CTX(index
, value
);
157 case CGS_IND_REG__DIDT
:
158 return WREG32_DIDT(index
, value
);
159 case CGS_IND_REG__AUDIO_ENDPT
:
160 DRM_ERROR("audio endpt register access not implemented.\n");
163 WARN(1, "Invalid indirect register space");
166 static uint8_t amdgpu_cgs_read_pci_config_byte(void *cgs_device
, unsigned addr
)
170 int ret
= pci_read_config_byte(adev
->pdev
, addr
, &val
);
171 if (WARN(ret
, "pci_read_config_byte error"))
176 static uint16_t amdgpu_cgs_read_pci_config_word(void *cgs_device
, unsigned addr
)
180 int ret
= pci_read_config_word(adev
->pdev
, addr
, &val
);
181 if (WARN(ret
, "pci_read_config_word error"))
186 static uint32_t amdgpu_cgs_read_pci_config_dword(void *cgs_device
,
191 int ret
= pci_read_config_dword(adev
->pdev
, addr
, &val
);
192 if (WARN(ret
, "pci_read_config_dword error"))
197 static void amdgpu_cgs_write_pci_config_byte(void *cgs_device
, unsigned addr
,
201 int ret
= pci_write_config_byte(adev
->pdev
, addr
, value
);
202 WARN(ret
, "pci_write_config_byte error");
205 static void amdgpu_cgs_write_pci_config_word(void *cgs_device
, unsigned addr
,
209 int ret
= pci_write_config_word(adev
->pdev
, addr
, value
);
210 WARN(ret
, "pci_write_config_word error");
213 static void amdgpu_cgs_write_pci_config_dword(void *cgs_device
, unsigned addr
,
217 int ret
= pci_write_config_dword(adev
->pdev
, addr
, value
);
218 WARN(ret
, "pci_write_config_dword error");
221 static const void *amdgpu_cgs_atom_get_data_table(void *cgs_device
,
222 unsigned table
, uint16_t *size
,
223 uint8_t *frev
, uint8_t *crev
)
228 if (amdgpu_atom_parse_data_header(
229 adev
->mode_info
.atom_context
, table
, size
,
230 frev
, crev
, &data_start
))
231 return (uint8_t*)adev
->mode_info
.atom_context
->bios
+
237 static int amdgpu_cgs_atom_get_cmd_table_revs(void *cgs_device
, unsigned table
,
238 uint8_t *frev
, uint8_t *crev
)
242 if (amdgpu_atom_parse_cmd_header(
243 adev
->mode_info
.atom_context
, table
,
250 static int amdgpu_cgs_atom_exec_cmd_table(void *cgs_device
, unsigned table
,
255 return amdgpu_atom_execute_table(
256 adev
->mode_info
.atom_context
, table
, args
);
259 static int amdgpu_cgs_create_pm_request(void *cgs_device
, cgs_handle_t
*request
)
265 static int amdgpu_cgs_destroy_pm_request(void *cgs_device
, cgs_handle_t request
)
271 static int amdgpu_cgs_set_pm_request(void *cgs_device
, cgs_handle_t request
,
278 static int amdgpu_cgs_pm_request_clock(void *cgs_device
, cgs_handle_t request
,
279 enum cgs_clock clock
, unsigned freq
)
285 static int amdgpu_cgs_pm_request_engine(void *cgs_device
, cgs_handle_t request
,
286 enum cgs_engine engine
, int powered
)
294 static int amdgpu_cgs_pm_query_clock_limits(void *cgs_device
,
295 enum cgs_clock clock
,
296 struct cgs_clock_limits
*limits
)
302 static int amdgpu_cgs_set_camera_voltages(void *cgs_device
, uint32_t mask
,
303 const uint32_t *voltages
)
305 DRM_ERROR("not implemented");
309 struct cgs_irq_params
{
311 cgs_irq_source_set_func_t set
;
312 cgs_irq_handler_func_t handler
;
316 static int cgs_set_irq_state(struct amdgpu_device
*adev
,
317 struct amdgpu_irq_src
*src
,
319 enum amdgpu_interrupt_state state
)
321 struct cgs_irq_params
*irq_params
=
322 (struct cgs_irq_params
*)src
->data
;
325 if (!irq_params
->set
)
327 return irq_params
->set(irq_params
->private_data
,
333 static int cgs_process_irq(struct amdgpu_device
*adev
,
334 struct amdgpu_irq_src
*source
,
335 struct amdgpu_iv_entry
*entry
)
337 struct cgs_irq_params
*irq_params
=
338 (struct cgs_irq_params
*)source
->data
;
341 if (!irq_params
->handler
)
343 return irq_params
->handler(irq_params
->private_data
,
348 static const struct amdgpu_irq_src_funcs cgs_irq_funcs
= {
349 .set
= cgs_set_irq_state
,
350 .process
= cgs_process_irq
,
353 static int amdgpu_cgs_add_irq_source(void *cgs_device
, unsigned src_id
,
355 cgs_irq_source_set_func_t set
,
356 cgs_irq_handler_func_t handler
,
361 struct cgs_irq_params
*irq_params
;
362 struct amdgpu_irq_src
*source
=
363 kzalloc(sizeof(struct amdgpu_irq_src
), GFP_KERNEL
);
367 kzalloc(sizeof(struct cgs_irq_params
), GFP_KERNEL
);
372 source
->num_types
= num_types
;
373 source
->funcs
= &cgs_irq_funcs
;
374 irq_params
->src_id
= src_id
;
375 irq_params
->set
= set
;
376 irq_params
->handler
= handler
;
377 irq_params
->private_data
= private_data
;
378 source
->data
= (void *)irq_params
;
379 ret
= amdgpu_irq_add_id(adev
, src_id
, source
);
388 static int amdgpu_cgs_irq_get(void *cgs_device
, unsigned src_id
, unsigned type
)
391 return amdgpu_irq_get(adev
, adev
->irq
.sources
[src_id
], type
);
394 static int amdgpu_cgs_irq_put(void *cgs_device
, unsigned src_id
, unsigned type
)
397 return amdgpu_irq_put(adev
, adev
->irq
.sources
[src_id
], type
);
400 static const struct cgs_ops amdgpu_cgs_ops
= {
401 amdgpu_cgs_gpu_mem_info
,
402 amdgpu_cgs_gmap_kmem
,
403 amdgpu_cgs_gunmap_kmem
,
404 amdgpu_cgs_alloc_gpu_mem
,
405 amdgpu_cgs_free_gpu_mem
,
406 amdgpu_cgs_gmap_gpu_mem
,
407 amdgpu_cgs_gunmap_gpu_mem
,
408 amdgpu_cgs_kmap_gpu_mem
,
409 amdgpu_cgs_kunmap_gpu_mem
,
410 amdgpu_cgs_read_register
,
411 amdgpu_cgs_write_register
,
412 amdgpu_cgs_read_ind_register
,
413 amdgpu_cgs_write_ind_register
,
414 amdgpu_cgs_read_pci_config_byte
,
415 amdgpu_cgs_read_pci_config_word
,
416 amdgpu_cgs_read_pci_config_dword
,
417 amdgpu_cgs_write_pci_config_byte
,
418 amdgpu_cgs_write_pci_config_word
,
419 amdgpu_cgs_write_pci_config_dword
,
420 amdgpu_cgs_atom_get_data_table
,
421 amdgpu_cgs_atom_get_cmd_table_revs
,
422 amdgpu_cgs_atom_exec_cmd_table
,
423 amdgpu_cgs_create_pm_request
,
424 amdgpu_cgs_destroy_pm_request
,
425 amdgpu_cgs_set_pm_request
,
426 amdgpu_cgs_pm_request_clock
,
427 amdgpu_cgs_pm_request_engine
,
428 amdgpu_cgs_pm_query_clock_limits
,
429 amdgpu_cgs_set_camera_voltages
432 static const struct cgs_os_ops amdgpu_cgs_os_ops
= {
433 amdgpu_cgs_import_gpu_mem
,
434 amdgpu_cgs_add_irq_source
,
439 void *amdgpu_cgs_create_device(struct amdgpu_device
*adev
)
441 struct amdgpu_cgs_device
*cgs_device
=
442 kmalloc(sizeof(*cgs_device
), GFP_KERNEL
);
445 DRM_ERROR("Couldn't allocate CGS device structure\n");
449 cgs_device
->base
.ops
= &amdgpu_cgs_ops
;
450 cgs_device
->base
.os_ops
= &amdgpu_cgs_os_ops
;
451 cgs_device
->adev
= adev
;
456 void amdgpu_cgs_destroy_device(void *cgs_device
)