2 * Copyright (c) 2016-2017, The Linux Foundation. All rights reserved.
3 * Copyright (C) 2013 Red Hat
4 * Author: Rob Clark <robdclark@gmail.com>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program. If not, see <http://www.gnu.org/licenses/>.
22 #include <linux/kernel.h>
23 #include <linux/clk.h>
24 #include <linux/cpufreq.h>
25 #include <linux/module.h>
26 #include <linux/component.h>
27 #include <linux/platform_device.h>
29 #include <linux/pm_runtime.h>
30 #include <linux/slab.h>
31 #include <linux/list.h>
32 #include <linux/iommu.h>
33 #include <linux/types.h>
34 #include <linux/of_graph.h>
35 #include <linux/of_device.h>
36 #include <linux/sde_io_util.h>
37 #include <asm/sizes.h>
38 #include <linux/kthread.h>
41 #include <mach/board.h>
42 #include <mach/socinfo.h>
43 #include <mach/iommu_domains.h>
47 #include <drm/drm_atomic.h>
48 #include <drm/drm_atomic_helper.h>
49 #include <drm/drm_crtc_helper.h>
50 #include <drm/drm_plane_helper.h>
51 #include <drm/drm_fb_helper.h>
52 #include <drm/msm_drm.h>
53 #include <drm/drm_gem.h>
55 #include "sde_power_handle.h"
57 #define GET_MAJOR_REV(rev) ((rev) >> 28)
58 #define GET_MINOR_REV(rev) (((rev) >> 16) & 0xFFF)
59 #define GET_STEP_REV(rev) ((rev) & 0xFFFF)
65 struct msm_perf_state;
66 struct msm_gem_submit;
67 struct msm_gem_address_space;
70 #define NUM_DOMAINS 4 /* one for KMS, then one per gpu core (?) */
73 #define MAX_ENCODERS 8
75 #define MAX_CONNECTORS 8
77 struct msm_file_private {
78 struct msm_gem_address_space *aspace;
79 struct list_head counters;
82 enum msm_mdp_plane_property {
83 /* blob properties, always put these first */
88 PLANE_PROP_SCALER_LUT_ED,
89 PLANE_PROP_SCALER_LUT_CIR,
90 PLANE_PROP_SCALER_LUT_SEP,
91 PLANE_PROP_SKIN_COLOR,
93 PLANE_PROP_FOLIAGE_COLOR,
95 /* # of blob properties */
98 /* range properties */
99 PLANE_PROP_ZPOS = PLANE_PROP_BLOBCOUNT,
101 PLANE_PROP_COLOR_FILL,
102 PLANE_PROP_H_DECIMATE,
103 PLANE_PROP_V_DECIMATE,
104 PLANE_PROP_INPUT_FENCE,
105 PLANE_PROP_HUE_ADJUST,
106 PLANE_PROP_SATURATION_ADJUST,
107 PLANE_PROP_VALUE_ADJUST,
108 PLANE_PROP_CONTRAST_ADJUST,
110 /* enum/bitmask properties */
113 PLANE_PROP_SRC_CONFIG,
115 /* total # of properties */
119 enum msm_mdp_crtc_property {
122 /* # of blob properties */
125 /* range properties */
126 CRTC_PROP_INPUT_FENCE_TIMEOUT = CRTC_PROP_BLOBCOUNT,
127 CRTC_PROP_OUTPUT_FENCE,
128 CRTC_PROP_OUTPUT_FENCE_OFFSET,
133 /* total # of properties */
137 enum msm_mdp_conn_property {
138 /* blob properties, always put these first */
139 CONNECTOR_PROP_SDE_INFO,
141 /* # of blob properties */
142 CONNECTOR_PROP_BLOBCOUNT,
144 /* range properties */
145 CONNECTOR_PROP_OUT_FB = CONNECTOR_PROP_BLOBCOUNT,
146 CONNECTOR_PROP_RETIRE_FENCE,
147 CONNECTOR_PROP_DST_X,
148 CONNECTOR_PROP_DST_Y,
149 CONNECTOR_PROP_DST_W,
150 CONNECTOR_PROP_DST_H,
151 CONNECTOR_PROP_PLL_DELTA,
153 /* enum/bitmask properties */
154 CONNECTOR_PROP_TOPOLOGY_NAME,
155 CONNECTOR_PROP_TOPOLOGY_CONTROL,
157 /* total # of properties */
161 struct msm_vblank_ctrl {
162 struct kthread_work work;
163 struct list_head event_list;
167 #define MAX_H_TILES_PER_DISPLAY 2
170 * enum msm_display_compression - compression method used for pixel stream
171 * @MSM_DISPLAY_COMPRESS_NONE: Pixel data is not compressed
172 * @MSM_DISPLAY_COMPRESS_DSC: DSC compresison is used
173 * @MSM_DISPLAY_COMPRESS_FBC: FBC compression is used
175 enum msm_display_compression {
176 MSM_DISPLAY_COMPRESS_NONE,
177 MSM_DISPLAY_COMPRESS_DSC,
178 MSM_DISPLAY_COMPRESS_FBC,
182 * enum msm_display_caps - features/capabilities supported by displays
183 * @MSM_DISPLAY_CAP_VID_MODE: Video or "active" mode supported
184 * @MSM_DISPLAY_CAP_CMD_MODE: Command mode supported
185 * @MSM_DISPLAY_CAP_HOT_PLUG: Hot plug detection supported
186 * @MSM_DISPLAY_CAP_EDID: EDID supported
188 enum msm_display_caps {
189 MSM_DISPLAY_CAP_VID_MODE = BIT(0),
190 MSM_DISPLAY_CAP_CMD_MODE = BIT(1),
191 MSM_DISPLAY_CAP_HOT_PLUG = BIT(2),
192 MSM_DISPLAY_CAP_EDID = BIT(3),
196 * struct msm_display_info - defines display properties
197 * @intf_type: DRM_MODE_CONNECTOR_ display type
198 * @capabilities: Bitmask of display flags
199 * @num_of_h_tiles: Number of horizontal tiles in case of split interface
200 * @h_tile_instance: Controller instance used per tile. Number of elements is
201 * based on num_of_h_tiles
202 * @is_connected: Set to true if display is connected
203 * @width_mm: Physical width
204 * @height_mm: Physical height
205 * @max_width: Max width of display. In case of hot pluggable display
206 * this is max width supported by controller
207 * @max_height: Max height of display. In case of hot pluggable display
208 * this is max height supported by controller
209 * @compression: Compression supported by the display
211 struct msm_display_info {
213 uint32_t capabilities;
215 uint32_t num_of_h_tiles;
216 uint32_t h_tile_instance[MAX_H_TILES_PER_DISPLAY];
220 unsigned int width_mm;
221 unsigned int height_mm;
226 enum msm_display_compression compression;
230 * struct msm_drm_event - defines custom event notification struct
231 * @base: base object required for event notification by DRM framework.
232 * @event: event object required for event notification by DRM framework.
233 * @info: contains information of DRM object for which events has been
235 * @data: memory location which contains response payload for event.
237 struct msm_drm_event {
238 struct drm_pending_event base;
239 struct drm_event event;
240 struct drm_msm_event_req info;
244 /* Commit thread specific structure */
245 struct msm_drm_commit {
246 struct drm_device *dev;
247 struct task_struct *thread;
248 unsigned int crtc_id;
249 struct kthread_worker worker;
252 #define MSM_GPU_MAX_RINGS 4
254 struct msm_drm_private {
258 struct sde_power_handle phandle;
259 struct sde_power_client *pclient;
261 /* subordinate devices, if present: */
262 struct platform_device *gpu_pdev;
264 /* possibly this should be in the kms component, but it is
265 * shared by both mdp4 and mdp5..
269 /* eDP is for mdp5 only, but kms has not been created
270 * when edp_bind() and edp_init() are called. Here is the only
271 * place to keep the edp instance.
275 /* DSI is shared by mdp4 and mdp5 */
276 struct msm_dsi *dsi[2];
278 /* when we have more than one 'msm_gpu' these need to be an array: */
281 struct drm_fb_helper *fbdev;
283 uint32_t next_fence[MSM_GPU_MAX_RINGS];
284 uint32_t completed_fence[MSM_GPU_MAX_RINGS];
286 wait_queue_head_t fence_event;
288 struct msm_rd_state *rd;
289 struct msm_perf_state *perf;
291 /* list of GEM objects: */
292 struct list_head inactive_list;
294 struct workqueue_struct *wq;
296 /* callbacks deferred until bo is inactive: */
297 struct list_head fence_cbs;
299 /* crtcs pending async atomic updates: */
300 uint32_t pending_crtcs;
301 wait_queue_head_t pending_crtcs_event;
303 /* Registered address spaces.. currently this is fixed per # of
304 * iommu's. Ie. one for display block and one for gpu block.
305 * Eventually, to do per-process gpu pagetables, we'll want one
306 * of these per-process.
308 unsigned int num_aspaces;
309 struct msm_gem_address_space *aspace[NUM_DOMAINS];
311 unsigned int num_planes;
312 struct drm_plane *planes[MAX_PLANES];
314 unsigned int num_crtcs;
315 struct drm_crtc *crtcs[MAX_CRTCS];
317 struct msm_drm_commit disp_thread[MAX_CRTCS];
319 unsigned int num_encoders;
320 struct drm_encoder *encoders[MAX_ENCODERS];
322 unsigned int num_bridges;
323 struct drm_bridge *bridges[MAX_BRIDGES];
325 unsigned int num_connectors;
326 struct drm_connector *connectors[MAX_CONNECTORS];
329 struct drm_property *plane_property[PLANE_PROP_COUNT];
330 struct drm_property *crtc_property[CRTC_PROP_COUNT];
331 struct drm_property *conn_property[CONNECTOR_PROP_COUNT];
333 /* Color processing properties for the crtc */
334 struct drm_property **cp_property;
336 /* VRAM carveout, used when no IOMMU: */
340 /* NOTE: mm managed at the page level, size is in # of pages
341 * and position mm_node->start is in # of pages:
346 struct msm_vblank_ctrl vblank_ctrl;
348 /* list of clients waiting for events */
349 struct list_head client_event_list;
353 uint32_t pixel_format;
357 * Some GPU targets can support multiple ringbuffers and preempt between them.
358 * In order to do this without massive API changes we will steal two bits from
359 * the top of the fence and use them to identify the ringbuffer, (0x00000001 for
360 * riug 0, 0x40000001 for ring 1, 0x50000001 for ring 2, etc). If you are going
361 * to do a fence comparision you have to make sure you are only comparing
362 * against fences from the same ring, but since fences within a ringbuffer are
363 * still contigious you can still use straight comparisons (i.e 0x40000001 is
364 * older than 0x40000002). Mathmatically there will be 0x3FFFFFFF timestamps
365 * per ring or ~103 days of 120 interrupts per second (two interrupts per frame
368 #define FENCE_RING(_fence) ((_fence >> 30) & 3)
369 #define FENCE(_ring, _fence) ((((_ring) & 3) << 30) | ((_fence) & 0x3FFFFFFF))
371 static inline bool COMPARE_FENCE_LTE(uint32_t a, uint32_t b)
373 return ((FENCE_RING(a) == FENCE_RING(b)) && a <= b);
376 static inline bool COMPARE_FENCE_LT(uint32_t a, uint32_t b)
378 return ((FENCE_RING(a) == FENCE_RING(b)) && a < b);
381 /* callback from wq once fence has passed: */
382 struct msm_fence_cb {
383 struct work_struct work;
385 void (*func)(struct msm_fence_cb *cb);
388 void __msm_fence_worker(struct work_struct *work);
390 #define INIT_FENCE_CB(_cb, _func) do { \
391 INIT_WORK(&(_cb)->work, __msm_fence_worker); \
392 (_cb)->func = _func; \
395 int msm_atomic_commit(struct drm_device *dev,
396 struct drm_atomic_state *state, bool async);
398 int msm_wait_fence(struct drm_device *dev, uint32_t fence,
399 ktime_t *timeout, bool interruptible);
400 int msm_queue_fence_cb(struct drm_device *dev,
401 struct msm_fence_cb *cb, uint32_t fence);
402 void msm_update_fence(struct drm_device *dev, uint32_t fence);
404 void msm_gem_unmap_vma(struct msm_gem_address_space *aspace,
405 struct msm_gem_vma *vma, struct sg_table *sgt,
407 int msm_gem_map_vma(struct msm_gem_address_space *aspace,
408 struct msm_gem_vma *vma, struct sg_table *sgt,
409 void *priv, unsigned int flags);
411 void msm_gem_address_space_put(struct msm_gem_address_space *aspace);
413 /* For GPU and legacy display */
414 struct msm_gem_address_space *
415 msm_gem_address_space_create(struct device *dev, struct iommu_domain *domain,
416 int type, const char *name);
417 struct msm_gem_address_space *
418 msm_gem_address_space_create_instance(struct msm_mmu *parent, const char *name,
419 uint64_t start, uint64_t end);
421 /* For SDE display */
422 struct msm_gem_address_space *
423 msm_gem_smmu_address_space_create(struct device *dev, struct msm_mmu *mmu,
426 int msm_ioctl_gem_submit(struct drm_device *dev, void *data,
427 struct drm_file *file);
429 int msm_gem_mmap_obj(struct drm_gem_object *obj,
430 struct vm_area_struct *vma);
431 int msm_gem_mmap(struct file *filp, struct vm_area_struct *vma);
432 int msm_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf);
433 uint64_t msm_gem_mmap_offset(struct drm_gem_object *obj);
434 int msm_gem_get_iova_locked(struct drm_gem_object *obj,
435 struct msm_gem_address_space *aspace, uint64_t *iova);
436 int msm_gem_get_iova(struct drm_gem_object *obj,
437 struct msm_gem_address_space *aspace, uint64_t *iova);
438 uint64_t msm_gem_iova(struct drm_gem_object *obj,
439 struct msm_gem_address_space *aspace);
440 struct page **msm_gem_get_pages(struct drm_gem_object *obj);
441 void msm_gem_put_pages(struct drm_gem_object *obj);
442 void msm_gem_put_iova(struct drm_gem_object *obj,
443 struct msm_gem_address_space *aspace);
444 int msm_gem_dumb_create(struct drm_file *file, struct drm_device *dev,
445 struct drm_mode_create_dumb *args);
446 int msm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
447 uint32_t handle, uint64_t *offset);
448 struct sg_table *msm_gem_prime_get_sg_table(struct drm_gem_object *obj);
449 void *msm_gem_prime_vmap(struct drm_gem_object *obj);
450 void msm_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr);
451 int msm_gem_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma);
452 struct drm_gem_object *msm_gem_prime_import_sg_table(struct drm_device *dev,
453 struct dma_buf_attachment *attach, struct sg_table *sg);
454 int msm_gem_prime_pin(struct drm_gem_object *obj);
455 void msm_gem_prime_unpin(struct drm_gem_object *obj);
456 void *msm_gem_vaddr_locked(struct drm_gem_object *obj);
457 void *msm_gem_vaddr(struct drm_gem_object *obj);
458 int msm_gem_queue_inactive_cb(struct drm_gem_object *obj,
459 struct msm_fence_cb *cb);
460 void msm_gem_move_to_active(struct drm_gem_object *obj,
461 struct msm_gpu *gpu, bool write, uint32_t fence);
462 void msm_gem_move_to_inactive(struct drm_gem_object *obj);
463 int msm_gem_cpu_prep(struct drm_gem_object *obj, uint32_t op,
465 int msm_gem_cpu_fini(struct drm_gem_object *obj);
466 void msm_gem_free_object(struct drm_gem_object *obj);
467 int msm_gem_new_handle(struct drm_device *dev, struct drm_file *file,
468 uint32_t size, uint32_t flags, uint32_t *handle);
469 struct drm_gem_object *msm_gem_new(struct drm_device *dev,
470 uint32_t size, uint32_t flags);
471 struct drm_gem_object *msm_gem_import(struct drm_device *dev,
472 uint32_t size, struct sg_table *sgt);
473 void msm_gem_sync(struct drm_gem_object *obj, u32 op);
475 int msm_framebuffer_prepare(struct drm_framebuffer *fb,
476 struct msm_gem_address_space *aspace);
477 void msm_framebuffer_cleanup(struct drm_framebuffer *fb,
478 struct msm_gem_address_space *aspace);
479 uint32_t msm_framebuffer_iova(struct drm_framebuffer *fb,
480 struct msm_gem_address_space *aspace, int plane);
481 struct drm_gem_object *msm_framebuffer_bo(struct drm_framebuffer *fb, int plane);
482 const struct msm_format *msm_framebuffer_format(struct drm_framebuffer *fb);
483 struct drm_framebuffer *msm_framebuffer_init(struct drm_device *dev,
484 struct drm_mode_fb_cmd2 *mode_cmd, struct drm_gem_object **bos);
485 struct drm_framebuffer *msm_framebuffer_create(struct drm_device *dev,
486 struct drm_file *file, struct drm_mode_fb_cmd2 *mode_cmd);
488 struct drm_fb_helper *msm_fbdev_init(struct drm_device *dev);
491 int hdmi_modeset_init(struct hdmi *hdmi, struct drm_device *dev,
492 struct drm_encoder *encoder);
493 void __init hdmi_register(void);
494 void __exit hdmi_unregister(void);
497 void __init msm_edp_register(void);
498 void __exit msm_edp_unregister(void);
499 int msm_edp_modeset_init(struct msm_edp *edp, struct drm_device *dev,
500 struct drm_encoder *encoder);
503 enum msm_dsi_encoder_id {
504 MSM_DSI_VIDEO_ENCODER_ID = 0,
505 MSM_DSI_CMD_ENCODER_ID = 1,
506 MSM_DSI_ENCODER_NUM = 2
510 * msm_send_crtc_notification - notify user-space clients of crtc events.
511 * @crtc: crtc that is generating the event.
512 * @event: event that needs to be notified.
513 * @payload: payload for the event.
515 void msm_send_crtc_notification(struct drm_crtc *crtc,
516 struct drm_event *event, u8 *payload);
517 #ifdef CONFIG_DRM_MSM_DSI
518 void __init msm_dsi_register(void);
519 void __exit msm_dsi_unregister(void);
520 int msm_dsi_modeset_init(struct msm_dsi *msm_dsi, struct drm_device *dev,
521 struct drm_encoder *encoders[MSM_DSI_ENCODER_NUM]);
523 static inline void __init msm_dsi_register(void)
526 static inline void __exit msm_dsi_unregister(void)
529 static inline int msm_dsi_modeset_init(struct msm_dsi *msm_dsi,
530 struct drm_device *dev,
531 struct drm_encoder *encoders[MSM_DSI_ENCODER_NUM])
537 #ifdef CONFIG_DEBUG_FS
538 void msm_gem_describe(struct drm_gem_object *obj, struct seq_file *m);
539 void msm_gem_describe_objects(struct list_head *list, struct seq_file *m);
540 void msm_framebuffer_describe(struct drm_framebuffer *fb, struct seq_file *m);
541 int msm_debugfs_late_init(struct drm_device *dev);
542 int msm_rd_debugfs_init(struct drm_minor *minor);
543 void msm_rd_debugfs_cleanup(struct drm_minor *minor);
544 void msm_rd_dump_submit(struct msm_gem_submit *submit);
545 int msm_perf_debugfs_init(struct drm_minor *minor);
546 void msm_perf_debugfs_cleanup(struct drm_minor *minor);
548 static inline int msm_debugfs_late_init(struct drm_device *dev) { return 0; }
549 static inline void msm_rd_dump_submit(struct msm_gem_submit *submit) {}
552 void __iomem *msm_ioremap(struct platform_device *pdev, const char *name,
553 const char *dbgname);
554 void msm_iounmap(struct platform_device *dev, void __iomem *addr);
555 void msm_writel(u32 data, void __iomem *addr);
556 u32 msm_readl(const void __iomem *addr);
558 #define DBG(fmt, ...) DRM_DEBUG(fmt"\n", ##__VA_ARGS__)
559 #define VERB(fmt, ...) if (0) DRM_DEBUG(fmt"\n", ##__VA_ARGS__)
561 static inline bool fence_completed(struct drm_device *dev, uint32_t fence)
563 struct msm_drm_private *priv = dev->dev_private;
565 return priv->completed_fence[FENCE_RING(fence)] >= fence;
568 static inline int align_pitch(int width, int bpp)
570 int bytespp = (bpp + 7) / 8;
571 /* adreno needs pitch aligned to 32 pixels: */
572 return bytespp * ALIGN(width, 32);
575 /* for the generated headers: */
576 #define INVALID_IDX(idx) ({BUG(); 0;})
577 #define fui(x) ({BUG(); 0;})
578 #define util_float_to_half(x) ({BUG(); 0;})
581 #define FIELD(val, name) (((val) & name ## __MASK) >> name ## __SHIFT)
583 /* for conditionally setting boolean flag(s): */
584 #define COND(bool, val) ((bool) ? (val) : 0)
586 #endif /* __MSM_DRV_H__ */