xref: /openbmc/linux/drivers/gpu/drm/amd/display/dmub/dmub_srv.h (revision 06ff634c0dae791c17ceeeb60c74e14470d76898)
1 /*
2  * Copyright 2019 Advanced Micro Devices, Inc.
3  *
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:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
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.
21  *
22  * Authors: AMD
23  *
24  */
25 
26 #ifndef _DMUB_SRV_H_
27 #define _DMUB_SRV_H_
28 
29 /**
30  * DOC: DMUB interface and operation
31  *
32  * DMUB is the interface to the display DMCUB microcontroller on DCN hardware.
33  * It delegates hardware initialization and command submission to the
34  * microcontroller. DMUB is the shortname for DMCUB.
35  *
36  * This interface is not thread-safe. Ensure that all access to the interface
37  * is properly synchronized by the caller.
38  *
39  * Initialization and usage of the DMUB service should be done in the
40  * steps given below:
41  *
42  * 1. dmub_srv_create()
43  * 2. dmub_srv_has_hw_support()
44  * 3. dmub_srv_calc_region_info()
45  * 4. dmub_srv_hw_init()
46  *
47  * The call to dmub_srv_create() is required to use the server.
48  *
49  * The calls to dmub_srv_has_hw_support() and dmub_srv_calc_region_info()
50  * are helpers to query cache window size and allocate framebuffer(s)
51  * for the cache windows.
52  *
53  * The call to dmub_srv_hw_init() programs the DMCUB registers to prepare
54  * for command submission. Commands can be queued via dmub_srv_cmd_queue()
55  * and executed via dmub_srv_cmd_execute().
56  *
57  * If the queue is full the dmub_srv_wait_for_idle() call can be used to
58  * wait until the queue has been cleared.
59  *
60  * Destroying the DMUB service can be done by calling dmub_srv_destroy().
61  * This does not clear DMUB hardware state, only software state.
62  *
63  * The interface is intended to be standalone and should not depend on any
64  * other component within DAL.
65  */
66 
67 #include "inc/dmub_cmd.h"
68 
69 #if defined(__cplusplus)
70 extern "C" {
71 #endif
72 
73 /* Forward declarations */
74 struct dmub_srv;
75 struct dmub_srv_common_regs;
76 
77 /* enum dmub_status - return code for dmcub functions */
78 enum dmub_status {
79 	DMUB_STATUS_OK = 0,
80 	DMUB_STATUS_NO_CTX,
81 	DMUB_STATUS_QUEUE_FULL,
82 	DMUB_STATUS_TIMEOUT,
83 	DMUB_STATUS_INVALID,
84 };
85 
86 /* enum dmub_asic - dmub asic identifier */
87 enum dmub_asic {
88 	DMUB_ASIC_NONE = 0,
89 	DMUB_ASIC_DCN20,
90 	DMUB_ASIC_DCN21,
91 	DMUB_ASIC_MAX,
92 };
93 
94 /* enum dmub_window_id - dmub window identifier */
95 enum dmub_window_id {
96 	DMUB_WINDOW_0_INST_CONST = 0,
97 	DMUB_WINDOW_1_STACK,
98 	DMUB_WINDOW_2_BSS_DATA,
99 	DMUB_WINDOW_3_VBIOS,
100 	DMUB_WINDOW_4_MAILBOX,
101 	DMUB_WINDOW_5_TRACEBUFF,
102 	DMUB_WINDOW_6_FW_STATE,
103 	DMUB_WINDOW_7_SCRATCH_MEM,
104 	DMUB_WINDOW_TOTAL,
105 };
106 
107 /**
108  * struct dmub_region - dmub hw memory region
109  * @base: base address for region, must be 256 byte aligned
110  * @top: top address for region
111  */
112 struct dmub_region {
113 	uint32_t base;
114 	uint32_t top;
115 };
116 
117 /**
118  * struct dmub_window - dmub hw cache window
119  * @off: offset to the fb memory in gpu address space
120  * @r: region in uc address space for cache window
121  */
122 struct dmub_window {
123 	union dmub_addr offset;
124 	struct dmub_region region;
125 };
126 
127 /**
128  * struct dmub_fb - defines a dmub framebuffer memory region
129  * @cpu_addr: cpu virtual address for the region, NULL if invalid
130  * @gpu_addr: gpu virtual address for the region, NULL if invalid
131  * @size: size of the region in bytes, zero if invalid
132  */
133 struct dmub_fb {
134 	void *cpu_addr;
135 	uint64_t gpu_addr;
136 	uint32_t size;
137 };
138 
139 /**
140  * struct dmub_srv_region_params - params used for calculating dmub regions
141  * @inst_const_size: size of the fw inst const section
142  * @bss_data_size: size of the fw bss data section
143  * @vbios_size: size of the vbios data
144  * @fw_bss_data: raw firmware bss data section
145  */
146 struct dmub_srv_region_params {
147 	uint32_t inst_const_size;
148 	uint32_t bss_data_size;
149 	uint32_t vbios_size;
150 	const uint8_t *fw_inst_const;
151 	const uint8_t *fw_bss_data;
152 };
153 
154 /**
155  * struct dmub_srv_region_info - output region info from the dmub service
156  * @fb_size: required minimum fb size for all regions, aligned to 4096 bytes
157  * @num_regions: number of regions used by the dmub service
158  * @regions: region info
159  *
160  * The regions are aligned such that they can be all placed within the
161  * same framebuffer but they can also be placed into different framebuffers.
162  *
163  * The size of each region can be calculated by the caller:
164  * size = reg.top - reg.base
165  *
166  * Care must be taken when performing custom allocations to ensure that each
167  * region base address is 256 byte aligned.
168  */
169 struct dmub_srv_region_info {
170 	uint32_t fb_size;
171 	uint8_t num_regions;
172 	struct dmub_region regions[DMUB_WINDOW_TOTAL];
173 };
174 
175 /**
176  * struct dmub_srv_fb_params - parameters used for driver fb setup
177  * @region_info: region info calculated by dmub service
178  * @cpu_addr: base cpu address for the framebuffer
179  * @gpu_addr: base gpu virtual address for the framebuffer
180  */
181 struct dmub_srv_fb_params {
182 	const struct dmub_srv_region_info *region_info;
183 	void *cpu_addr;
184 	uint64_t gpu_addr;
185 };
186 
187 /**
188  * struct dmub_srv_fb_info - output fb info from the dmub service
189  * @num_fbs: number of required dmub framebuffers
190  * @fbs: fb data for each region
191  *
192  * Output from the dmub service helper that can be used by the
193  * driver to prepare dmub_fb that can be passed into the dmub
194  * hw init service.
195  *
196  * Assumes that all regions are within the same framebuffer
197  * and have been setup according to the region_info generated
198  * by the dmub service.
199  */
200 struct dmub_srv_fb_info {
201 	uint8_t num_fb;
202 	struct dmub_fb fb[DMUB_WINDOW_TOTAL];
203 };
204 
205 /**
206  * struct dmub_srv_base_funcs - Driver specific base callbacks
207  */
208 struct dmub_srv_base_funcs {
209 	/**
210 	 * @reg_read:
211 	 *
212 	 * Hook for reading a register.
213 	 *
214 	 * Return: The 32-bit register value from the given address.
215 	 */
216 	uint32_t (*reg_read)(void *ctx, uint32_t address);
217 
218 	/**
219 	 * @reg_write:
220 	 *
221 	 * Hook for writing a value to the register specified by address.
222 	 */
223 	void (*reg_write)(void *ctx, uint32_t address, uint32_t value);
224 };
225 
226 /**
227  * struct dmub_srv_hw_funcs - hardware sequencer funcs for dmub
228  */
229 struct dmub_srv_hw_funcs {
230 	/* private: internal use only */
231 
232 	void (*init)(struct dmub_srv *dmub);
233 
234 	void (*reset)(struct dmub_srv *dmub);
235 
236 	void (*reset_release)(struct dmub_srv *dmub);
237 
238 	void (*backdoor_load)(struct dmub_srv *dmub,
239 			      const struct dmub_window *cw0,
240 			      const struct dmub_window *cw1);
241 
242 	void (*setup_windows)(struct dmub_srv *dmub,
243 			      const struct dmub_window *cw2,
244 			      const struct dmub_window *cw3,
245 			      const struct dmub_window *cw4,
246 			      const struct dmub_window *cw5,
247 			      const struct dmub_window *cw6);
248 
249 	void (*setup_mailbox)(struct dmub_srv *dmub,
250 			      const struct dmub_region *inbox1);
251 
252 	uint32_t (*get_inbox1_rptr)(struct dmub_srv *dmub);
253 
254 	void (*set_inbox1_wptr)(struct dmub_srv *dmub, uint32_t wptr_offset);
255 
256 	bool (*is_supported)(struct dmub_srv *dmub);
257 
258 	bool (*is_hw_init)(struct dmub_srv *dmub);
259 
260 	bool (*is_phy_init)(struct dmub_srv *dmub);
261 
262 	bool (*is_auto_load_done)(struct dmub_srv *dmub);
263 
264 	void (*set_gpint)(struct dmub_srv *dmub,
265 			  union dmub_gpint_data_register reg);
266 
267 	bool (*is_gpint_acked)(struct dmub_srv *dmub,
268 			       union dmub_gpint_data_register reg);
269 
270 	uint32_t (*get_gpint_response)(struct dmub_srv *dmub);
271 };
272 
273 /**
274  * struct dmub_srv_create_params - params for dmub service creation
275  * @base_funcs: driver supplied base routines
276  * @hw_funcs: optional overrides for hw funcs
277  * @user_ctx: context data for callback funcs
278  * @asic: driver supplied asic
279  * @fw_version: the current firmware version, if any
280  * @is_virtual: false for hw support only
281  */
282 struct dmub_srv_create_params {
283 	struct dmub_srv_base_funcs funcs;
284 	struct dmub_srv_hw_funcs *hw_funcs;
285 	void *user_ctx;
286 	enum dmub_asic asic;
287 	uint32_t fw_version;
288 	bool is_virtual;
289 };
290 
291 /*
292  * struct dmub_srv_hw_params - params for dmub hardware initialization
293  * @fb: framebuffer info for each region
294  * @fb_base: base of the framebuffer aperture
295  * @fb_offset: offset of the framebuffer aperture
296  * @psp_version: psp version to pass for DMCU init
297  * @load_inst_const: true if DMUB should load inst const fw
298  */
299 struct dmub_srv_hw_params {
300 	struct dmub_fb *fb[DMUB_WINDOW_TOTAL];
301 	uint64_t fb_base;
302 	uint64_t fb_offset;
303 	uint32_t psp_version;
304 	bool load_inst_const;
305 };
306 
307 /**
308  * struct dmub_srv - software state for dmcub
309  * @asic: dmub asic identifier
310  * @user_ctx: user provided context for the dmub_srv
311  * @fw_version: the current firmware version, if any
312  * @is_virtual: false if hardware support only
313  * @fw_state: dmub firmware state pointer
314  */
315 struct dmub_srv {
316 	enum dmub_asic asic;
317 	void *user_ctx;
318 	uint32_t fw_version;
319 	bool is_virtual;
320 	struct dmub_fb scratch_mem_fb;
321 	volatile const struct dmub_fw_state *fw_state;
322 
323 	/* private: internal use only */
324 	const struct dmub_srv_common_regs *regs;
325 
326 	struct dmub_srv_base_funcs funcs;
327 	struct dmub_srv_hw_funcs hw_funcs;
328 	struct dmub_rb inbox1_rb;
329 
330 	bool sw_init;
331 	bool hw_init;
332 
333 	uint64_t fb_base;
334 	uint64_t fb_offset;
335 	uint32_t psp_version;
336 };
337 
338 /**
339  * DMUB firmware version helper macro - useful for checking if the version
340  * of a firmware to know if feature or functionality is supported or present.
341  */
342 #define DMUB_FW_VERSION(major, minor, revision) \
343 	((((major) & 0xFF) << 24) | (((minor) & 0xFF) << 16) | ((revision) & 0xFFFF))
344 
345 /**
346  * dmub_srv_create() - creates the DMUB service.
347  * @dmub: the dmub service
348  * @params: creation parameters for the service
349  *
350  * Return:
351  *   DMUB_STATUS_OK - success
352  *   DMUB_STATUS_INVALID - unspecified error
353  */
354 enum dmub_status dmub_srv_create(struct dmub_srv *dmub,
355 				 const struct dmub_srv_create_params *params);
356 
357 /**
358  * dmub_srv_destroy() - destroys the DMUB service.
359  * @dmub: the dmub service
360  */
361 void dmub_srv_destroy(struct dmub_srv *dmub);
362 
363 /**
364  * dmub_srv_calc_region_info() - retreives region info from the dmub service
365  * @dmub: the dmub service
366  * @params: parameters used to calculate region locations
367  * @info_out: the output region info from dmub
368  *
369  * Calculates the base and top address for all relevant dmub regions
370  * using the parameters given (if any).
371  *
372  * Return:
373  *   DMUB_STATUS_OK - success
374  *   DMUB_STATUS_INVALID - unspecified error
375  */
376 enum dmub_status
377 dmub_srv_calc_region_info(struct dmub_srv *dmub,
378 			  const struct dmub_srv_region_params *params,
379 			  struct dmub_srv_region_info *out);
380 
381 /**
382  * dmub_srv_calc_region_info() - retreives fb info from the dmub service
383  * @dmub: the dmub service
384  * @params: parameters used to calculate fb locations
385  * @info_out: the output fb info from dmub
386  *
387  * Calculates the base and top address for all relevant dmub regions
388  * using the parameters given (if any).
389  *
390  * Return:
391  *   DMUB_STATUS_OK - success
392  *   DMUB_STATUS_INVALID - unspecified error
393  */
394 enum dmub_status dmub_srv_calc_fb_info(struct dmub_srv *dmub,
395 				       const struct dmub_srv_fb_params *params,
396 				       struct dmub_srv_fb_info *out);
397 
398 /**
399  * dmub_srv_has_hw_support() - returns hw support state for dmcub
400  * @dmub: the dmub service
401  * @is_supported: hw support state
402  *
403  * Queries the hardware for DMCUB support and returns the result.
404  *
405  * Can be called before dmub_srv_hw_init().
406  *
407  * Return:
408  *   DMUB_STATUS_OK - success
409  *   DMUB_STATUS_INVALID - unspecified error
410  */
411 enum dmub_status dmub_srv_has_hw_support(struct dmub_srv *dmub,
412 					 bool *is_supported);
413 
414 /**
415  * dmub_srv_is_hw_init() - returns hardware init state
416  *
417  * Return:
418  *   DMUB_STATUS_OK - success
419  *   DMUB_STATUS_INVALID - unspecified error
420  */
421 enum dmub_status dmub_srv_is_hw_init(struct dmub_srv *dmub, bool *is_hw_init);
422 
423 /**
424  * dmub_srv_hw_init() - initializes the underlying DMUB hardware
425  * @dmub: the dmub service
426  * @params: params for hardware initialization
427  *
428  * Resets the DMUB hardware and performs backdoor loading of the
429  * required cache regions based on the input framebuffer regions.
430  *
431  * Return:
432  *   DMUB_STATUS_OK - success
433  *   DMUB_STATUS_NO_CTX - dmcub context not initialized
434  *   DMUB_STATUS_INVALID - unspecified error
435  */
436 enum dmub_status dmub_srv_hw_init(struct dmub_srv *dmub,
437 				  const struct dmub_srv_hw_params *params);
438 
439 /**
440  * dmub_srv_hw_reset() - puts the DMUB hardware in reset state if initialized
441  * @dmub: the dmub service
442  *
443  * Before destroying the DMUB service or releasing the backing framebuffer
444  * memory we'll need to put the DMCUB into reset first.
445  *
446  * A subsequent call to dmub_srv_hw_init() will re-enable the DMCUB.
447  *
448  * Return:
449  *   DMUB_STATUS_OK - success
450  *   DMUB_STATUS_INVALID - unspecified error
451  */
452 enum dmub_status dmub_srv_hw_reset(struct dmub_srv *dmub);
453 
454 /**
455  * dmub_srv_cmd_queue() - queues a command to the DMUB
456  * @dmub: the dmub service
457  * @cmd: the command to queue
458  *
459  * Queues a command to the DMUB service but does not begin execution
460  * immediately.
461  *
462  * Return:
463  *   DMUB_STATUS_OK - success
464  *   DMUB_STATUS_QUEUE_FULL - no remaining room in queue
465  *   DMUB_STATUS_INVALID - unspecified error
466  */
467 enum dmub_status dmub_srv_cmd_queue(struct dmub_srv *dmub,
468 				    const union dmub_rb_cmd *cmd);
469 
470 /**
471  * dmub_srv_cmd_execute() - Executes a queued sequence to the dmub
472  * @dmub: the dmub service
473  *
474  * Begins execution of queued commands on the dmub.
475  *
476  * Return:
477  *   DMUB_STATUS_OK - success
478  *   DMUB_STATUS_INVALID - unspecified error
479  */
480 enum dmub_status dmub_srv_cmd_execute(struct dmub_srv *dmub);
481 
482 /**
483  * dmub_srv_wait_for_auto_load() - Waits for firmware auto load to complete
484  * @dmub: the dmub service
485  * @timeout_us: the maximum number of microseconds to wait
486  *
487  * Waits until firmware has been autoloaded by the DMCUB. The maximum
488  * wait time is given in microseconds to prevent spinning forever.
489  *
490  * On ASICs without firmware autoload support this function will return
491  * immediately.
492  *
493  * Return:
494  *   DMUB_STATUS_OK - success
495  *   DMUB_STATUS_TIMEOUT - wait for phy init timed out
496  *   DMUB_STATUS_INVALID - unspecified error
497  */
498 enum dmub_status dmub_srv_wait_for_auto_load(struct dmub_srv *dmub,
499 					     uint32_t timeout_us);
500 
501 /**
502  * dmub_srv_wait_for_phy_init() - Waits for DMUB PHY init to complete
503  * @dmub: the dmub service
504  * @timeout_us: the maximum number of microseconds to wait
505  *
506  * Waits until the PHY has been initialized by the DMUB. The maximum
507  * wait time is given in microseconds to prevent spinning forever.
508  *
509  * On ASICs without PHY init support this function will return
510  * immediately.
511  *
512  * Return:
513  *   DMUB_STATUS_OK - success
514  *   DMUB_STATUS_TIMEOUT - wait for phy init timed out
515  *   DMUB_STATUS_INVALID - unspecified error
516  */
517 enum dmub_status dmub_srv_wait_for_phy_init(struct dmub_srv *dmub,
518 					    uint32_t timeout_us);
519 
520 /**
521  * dmub_srv_wait_for_idle() - Waits for the DMUB to be idle
522  * @dmub: the dmub service
523  * @timeout_us: the maximum number of microseconds to wait
524  *
525  * Waits until the DMUB buffer is empty and all commands have
526  * finished processing. The maximum wait time is given in
527  * microseconds to prevent spinning forever.
528  *
529  * Return:
530  *   DMUB_STATUS_OK - success
531  *   DMUB_STATUS_TIMEOUT - wait for buffer to flush timed out
532  *   DMUB_STATUS_INVALID - unspecified error
533  */
534 enum dmub_status dmub_srv_wait_for_idle(struct dmub_srv *dmub,
535 					uint32_t timeout_us);
536 
537 /**
538  * dmub_srv_send_gpint_command() - Sends a GPINT based command.
539  * @dmub: the dmub service
540  * @command_code: the command code to send
541  * @param: the command parameter to send
542  * @timeout_us: the maximum number of microseconds to wait
543  *
544  * Sends a command via the general purpose interrupt (GPINT).
545  * Waits for the number of microseconds specified by timeout_us
546  * for the command ACK before returning.
547  *
548  * Can be called after software initialization.
549  *
550  * Return:
551  *   DMUB_STATUS_OK - success
552  *   DMUB_STATUS_TIMEOUT - wait for ACK timed out
553  *   DMUB_STATUS_INVALID - unspecified error
554  */
555 enum dmub_status
556 dmub_srv_send_gpint_command(struct dmub_srv *dmub,
557 			    enum dmub_gpint_command command_code,
558 			    uint16_t param, uint32_t timeout_us);
559 
560 /**
561  * dmub_srv_get_gpint_response() - Queries the GPINT response.
562  * @dmub: the dmub service
563  * @response: the response for the last GPINT
564  *
565  * Returns the response code for the last GPINT interrupt.
566  *
567  * Can be called after software initialization.
568  *
569  * Return:
570  *   DMUB_STATUS_OK - success
571  *   DMUB_STATUS_INVALID - unspecified error
572  */
573 enum dmub_status dmub_srv_get_gpint_response(struct dmub_srv *dmub,
574 					     uint32_t *response);
575 
576 /**
577  * dmub_flush_buffer_mem() - Read back entire frame buffer region.
578  * This ensures that the write from x86 has been flushed and will not
579  * hang the DMCUB.
580  * @fb: frame buffer to flush
581  *
582  * Can be called after software initialization.
583  */
584 void dmub_flush_buffer_mem(const struct dmub_fb *fb);
585 
586 #if defined(__cplusplus)
587 }
588 #endif
589 
590 #endif /* _DMUB_SRV_H_ */
591