Commit f39db26c authored by Sui Jingfeng's avatar Sui Jingfeng

drm: Add kms driver for loongson display controller

Loongson display controller IP has been integrated in both Loongson north
bridge chipset (ls7a1000/ls7a2000) and Loongson SoCs (ls2k1000/ls2k2000).
It has even been included in Loongson's BMC products. It has two display
pipes, and each display pipe supports a primary plane and a cursor plane.

For the DC in the LS7a1000, each display pipe has a DVO output interface,
which is able to support 1920x1080@60Hz. For the DC in the LS7A2000, each
display pipe is equipped with a built-in HDMI encoder, which is compliant
with the HDMI 1.4 specification. The first display pipe is also equipped
with a transparent VGA encoder, which is parallel with the HDMI encoder.
To get a decent performance for writing framebuffer data to the VRAM, the
write combine support should be enabled.

v1 -> v2:
 1) Use hpd status reg when polling for ls7a2000.
 2) Fix all warnings that emerged when compiling with W=1.

v2 -> v3:
 1) Add COMPILE_TEST to Kconfig and make the driver off by default
 2) Alphabetical sorting headers (Thomas)
 3) Untangle register access functions as much as possible (Thomas)
 4) Switch to TTM-based memory manager (Thomas)
 5) Add the chip ID detection function which can be used to distinguish
    chip models
 6) Revise the built-in HDMI phy driver, nearly all main stream mode below
    4K@30Hz is tested, and this driver supports clone(mirror) display mode
    and extend(joint) display mode.

v3 -> v4:
 1) Quickly fix a small mistake.

v4 -> v5:
 1) Add per display pipe debugfs support to the builtin HDMI encoder.

v5 -> v6:
 1) Remove stray code which didn't get used, say lsdc_of_get_reserved_ram
 2) Fix all typos I could found, make sentences and code more readable
 3) Untangle lsdc_hdmi*_connector_detect() function according to the pipe
 4) Rename this driver as loongson.

v6 -> v7:
1) Add prime support for buffer self-sharing, sharing buffer with
   drm/etnaviv is also tested and it works with limitations.
2) Implement buffer object tracking with list_head.
3) Add S3(sleep to RAM) support
4) Rewrite lsdc_bo_move since TTM core stop allocating resources
    during BO creation. Patch V1 ~ V6 of this series no longer work.
    Thus, we send V7.

v7 -> v8:
 1) Zero a compile warning on a 32-bit platform, compile with W=1
 2) Revise lsdc_bo_gpu_offset() and make minor cleanups.
 3) Pageflip tested on the virtual terminal with the following commands:

    modetest -M loongson -s 32:1920x1080 -v
    modetest -M loongson -s 34:1920x1080 -v -F tiles

   It works like a charm, when running the pageflip test with dual screens
   configuration, another two additional BOs were created by the modetest,
   VRAM usage up to 40+ MB, well we have at least 64MB, still enough.

   # cat bos

       bo[0000]: size:     8112kB VRAM
       bo[0001]: size:       16kB VRAM
       bo[0002]: size:       16kB VRAM
       bo[0003]: size:    16208kB VRAM
       bo[0004]: size:     8112kB VRAM
       bo[0005]: size:     8112kB VRAM

v8 -> v9:
 1) Select I2C and I2C_ALGOBIT in Kconfig, should depend on MMU.
 2) Using pci_get_domain_bus_and_slot to get the GPU device.

v9 -> v10:
 1) Revise lsdc_drm_freeze() to implement S3 correctly. We realized that
    the pinned BO could not be moved, the VRAM lost power when sleeping
    to RAM. Thus, the data in the buffer who is pinned in VRAM will get
    lost when resumed. Yet it's not a big problem because this driver
    relies on the CPU to update the front framebuffer. We can see the
    garbage data when resume from S3, but the screen will show the right
    image as I move the cursor. This is due to the CPU repaint. v10 of
    this patch makes S3 perfect by unpin all of the BOs in VRAM, evict
    them all to system RAM in lsdc_drm_freeze().

v10 -> v11:
 1) On a double-screen case, The buffer object backing the single giant
    framebuffer is referenced by two GEM objects; hence, it will be
    pinned at least twice by prepare_fb() function. This causes its pin
    count > 1. V10 of this patch only unpins VRAM BOs once when suspend,
    which is not correct on double-screen case. V11 of this patch unpin
    the BOs until its pin count reaches zero when suspend. Then, we make
    the S3 support complete finally. With v11, I can't see any garbage
    data when resume.

 2) Fix vblank wait timeout when disable CRTC.
 3) Test against IGT, at least fbdev test and kms_flip test passed.
 4) Rewrite pixel PLL update function, magic numbers eliminated (Emil)
 5) Drop a few common hardware features description in lsdc_desc (Emil)
 6) Drop lsdc_mode_config_mode_valid(), instead add restrictions in dumb
    create function. (Emil)
 7) Untangle the ls7a1000 case and ls7a2000 case completely (Thomas)

v11 -> v12:
 none

v12 -> v13:
 1) Add benchmarks to figure out the bandwidth of the hardware platform.
    Usage:
    # cd /sys/kernel/debug/dri/0/
    # cat benchmark

 2) VRAM is filled with garbage data if uninitialized, add a buffer
    clearing procedure (lsdc_bo_clear), clear the BO on creation time.
 3) Update copyrights and adjust coding style (Huacai)

v13 -> v14:
 1) Trying to add async update support for cursor plane.

v14 -> v15:
 1) Add lsdc_vga_set_decode() funciton, which allow us remove multi-video
    cards workaround, now it allow drm/loongson, drm/amdgpu, drm/etnaviv
    co-exist in the system, more is also possible (Emil and Xuerui)
 2) Fix typos and grammar mistakes as much as possible (Xuerui)
 3) Unify copyrights as GPL-2.0+ (Xuerui)
 4) Fix a bug introduce since V13, TTM may import BO from other drivers,
    we shouldn't clear it on such a case.

Cc: Maarten Lankhorst <maarten.lankhorst@linux.intel.com>
Cc: Maxime Ripard <mripard@kernel.org>
Cc: Thomas Zimmermann <tzimmermann@suse.de>
Cc: David Airlie <airlied@gmail.com>
Cc: Daniel Vetter <daniel@ffwll.ch>
Cc: Sumit Semwal <sumit.semwal@linaro.org>
Cc: "Christian König" <christian.koenig@amd.com>
Cc: Nathan Chancellor <nathan@kernel.org>
Cc: Emil Velikov <emil.l.velikov@gmail.com>
Cc: Geert Uytterhoeven <geert+renesas@glider.be>
Cc: loongson-kernel@lists.loongnix.cn
Tested-by: default avatarLiu Peibao <liupeibao@loongson.cn>
Tested-by: default avatarLi Yi  <liyi@loongson.cn>
Acked-by: default avatarThomas Zimmermann <tzimmermann@suse.de>
Signed-off-by: default avatarSui Jingfeng <suijingfeng@loongson.cn>
Link: https://patchwork.freedesktop.org/patch/msgid/20230615143613.1236245-2-15330273260@189.cn
parent 319eeec5
......@@ -323,6 +323,8 @@ source "drivers/gpu/drm/v3d/Kconfig"
source "drivers/gpu/drm/vc4/Kconfig"
source "drivers/gpu/drm/loongson/Kconfig"
source "drivers/gpu/drm/etnaviv/Kconfig"
source "drivers/gpu/drm/hisilicon/Kconfig"
......
......@@ -194,3 +194,4 @@ obj-y += gud/
obj-$(CONFIG_DRM_HYPERV) += hyperv/
obj-y += solomon/
obj-$(CONFIG_DRM_SPRD) += sprd/
obj-$(CONFIG_DRM_LOONGSON) += loongson/
# SPDX-License-Identifier: GPL-2.0
config DRM_LOONGSON
tristate "DRM support for Loongson Graphics"
depends on DRM && PCI && MMU
select DRM_KMS_HELPER
select DRM_TTM
select I2C
select I2C_ALGOBIT
help
This is a DRM driver for Loongson Graphics, it may including
LS7A2000, LS7A1000, LS2K2000 and LS2K1000 etc. Loongson LS7A
series are bridge chipset, while Loongson LS2K series are SoC.
If "M" is selected, the module will be called loongson.
If in doubt, say "N".
# SPDX-License-Identifier: GPL-2.0
loongson-y := \
lsdc_benchmark.o \
lsdc_crtc.o \
lsdc_debugfs.o \
lsdc_drv.o \
lsdc_gem.o \
lsdc_gfxpll.o \
lsdc_i2c.o \
lsdc_irq.o \
lsdc_output_7a1000.o \
lsdc_output_7a2000.o \
lsdc_plane.o \
lsdc_pixpll.o \
lsdc_probe.o \
lsdc_ttm.o
loongson-y += loongson_device.o \
loongson_module.o
obj-$(CONFIG_DRM_LOONGSON) += loongson.o
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright (C) 2023 Loongson Technology Corporation Limited
*/
#include <linux/pci.h>
#include "lsdc_drv.h"
static const struct lsdc_kms_funcs ls7a1000_kms_funcs = {
.create_i2c = lsdc_create_i2c_chan,
.irq_handler = ls7a1000_dc_irq_handler,
.output_init = ls7a1000_output_init,
.cursor_plane_init = ls7a1000_cursor_plane_init,
.primary_plane_init = lsdc_primary_plane_init,
.crtc_init = ls7a1000_crtc_init,
};
static const struct lsdc_kms_funcs ls7a2000_kms_funcs = {
.create_i2c = lsdc_create_i2c_chan,
.irq_handler = ls7a2000_dc_irq_handler,
.output_init = ls7a2000_output_init,
.cursor_plane_init = ls7a2000_cursor_plane_init,
.primary_plane_init = lsdc_primary_plane_init,
.crtc_init = ls7a2000_crtc_init,
};
static const struct loongson_gfx_desc ls7a1000_gfx = {
.dc = {
.num_of_crtc = 2,
.max_pixel_clk = 200000,
.max_width = 2048,
.max_height = 2048,
.num_of_hw_cursor = 1,
.hw_cursor_w = 32,
.hw_cursor_h = 32,
.pitch_align = 256,
.has_vblank_counter = false,
.funcs = &ls7a1000_kms_funcs,
},
.conf_reg_base = LS7A1000_CONF_REG_BASE,
.gfxpll = {
.reg_offset = LS7A1000_PLL_GFX_REG,
.reg_size = 8,
},
.pixpll = {
[0] = {
.reg_offset = LS7A1000_PIXPLL0_REG,
.reg_size = 8,
},
[1] = {
.reg_offset = LS7A1000_PIXPLL1_REG,
.reg_size = 8,
},
},
.chip_id = CHIP_LS7A1000,
.model = "LS7A1000 bridge chipset",
};
static const struct loongson_gfx_desc ls7a2000_gfx = {
.dc = {
.num_of_crtc = 2,
.max_pixel_clk = 350000,
.max_width = 4096,
.max_height = 4096,
.num_of_hw_cursor = 2,
.hw_cursor_w = 64,
.hw_cursor_h = 64,
.pitch_align = 64,
.has_vblank_counter = true,
.funcs = &ls7a2000_kms_funcs,
},
.conf_reg_base = LS7A2000_CONF_REG_BASE,
.gfxpll = {
.reg_offset = LS7A2000_PLL_GFX_REG,
.reg_size = 8,
},
.pixpll = {
[0] = {
.reg_offset = LS7A2000_PIXPLL0_REG,
.reg_size = 8,
},
[1] = {
.reg_offset = LS7A2000_PIXPLL1_REG,
.reg_size = 8,
},
},
.chip_id = CHIP_LS7A2000,
.model = "LS7A2000 bridge chipset",
};
static const struct lsdc_desc *__chip_id_desc_table[] = {
[CHIP_LS7A1000] = &ls7a1000_gfx.dc,
[CHIP_LS7A2000] = &ls7a2000_gfx.dc,
[CHIP_LS_LAST] = NULL,
};
const struct lsdc_desc *
lsdc_device_probe(struct pci_dev *pdev, enum loongson_chip_id chip_id)
{
return __chip_id_desc_table[chip_id];
}
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright (C) 2023 Loongson Technology Corporation Limited
*/
#include <linux/pci.h>
#include <video/nomodeset.h>
#include "loongson_module.h"
static int loongson_modeset = -1;
MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
module_param_named(modeset, loongson_modeset, int, 0400);
int loongson_vblank = 1;
MODULE_PARM_DESC(vblank, "Disable/Enable hw vblank support");
module_param_named(vblank, loongson_vblank, int, 0400);
static int __init loongson_module_init(void)
{
if (!loongson_modeset || video_firmware_drivers_only())
return -ENODEV;
return pci_register_driver(&lsdc_pci_driver);
}
module_init(loongson_module_init);
static void __exit loongson_module_exit(void)
{
pci_unregister_driver(&lsdc_pci_driver);
}
module_exit(loongson_module_exit);
/* SPDX-License-Identifier: GPL-2.0+ */
/*
* Copyright (C) 2023 Loongson Technology Corporation Limited
*/
#ifndef __LOONGSON_MODULE_H__
#define __LOONGSON_MODULE_H__
extern int loongson_vblank;
extern struct pci_driver lsdc_pci_driver;
#endif
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright (C) 2023 Loongson Technology Corporation Limited
*/
#include <drm/drm_debugfs.h>
#include "lsdc_benchmark.h"
#include "lsdc_drv.h"
#include "lsdc_gem.h"
#include "lsdc_ttm.h"
typedef void (*lsdc_copy_proc_t)(struct lsdc_bo *src_bo,
struct lsdc_bo *dst_bo,
unsigned int size,
int n);
static void lsdc_copy_gtt_to_vram_cpu(struct lsdc_bo *src_bo,
struct lsdc_bo *dst_bo,
unsigned int size,
int n)
{
lsdc_bo_kmap(src_bo);
lsdc_bo_kmap(dst_bo);
while (n--)
memcpy_toio(dst_bo->kptr, src_bo->kptr, size);
lsdc_bo_kunmap(src_bo);
lsdc_bo_kunmap(dst_bo);
}
static void lsdc_copy_vram_to_gtt_cpu(struct lsdc_bo *src_bo,
struct lsdc_bo *dst_bo,
unsigned int size,
int n)
{
lsdc_bo_kmap(src_bo);
lsdc_bo_kmap(dst_bo);
while (n--)
memcpy_fromio(dst_bo->kptr, src_bo->kptr, size);
lsdc_bo_kunmap(src_bo);
lsdc_bo_kunmap(dst_bo);
}
static void lsdc_copy_gtt_to_gtt_cpu(struct lsdc_bo *src_bo,
struct lsdc_bo *dst_bo,
unsigned int size,
int n)
{
lsdc_bo_kmap(src_bo);
lsdc_bo_kmap(dst_bo);
while (n--)
memcpy(dst_bo->kptr, src_bo->kptr, size);
lsdc_bo_kunmap(src_bo);
lsdc_bo_kunmap(dst_bo);
}
static void lsdc_benchmark_copy(struct lsdc_device *ldev,
unsigned int size,
unsigned int n,
u32 src_domain,
u32 dst_domain,
lsdc_copy_proc_t copy_proc,
struct drm_printer *p)
{
struct drm_device *ddev = &ldev->base;
struct lsdc_bo *src_bo;
struct lsdc_bo *dst_bo;
unsigned long start_jiffies;
unsigned long end_jiffies;
unsigned int throughput;
unsigned int time;
src_bo = lsdc_bo_create_kernel_pinned(ddev, src_domain, size);
dst_bo = lsdc_bo_create_kernel_pinned(ddev, dst_domain, size);
start_jiffies = jiffies;
copy_proc(src_bo, dst_bo, size, n);
end_jiffies = jiffies;
lsdc_bo_free_kernel_pinned(src_bo);
lsdc_bo_free_kernel_pinned(dst_bo);
time = jiffies_to_msecs(end_jiffies - start_jiffies);
throughput = (n * (size >> 10)) / time;
drm_printf(p,
"Copy bo of %uKiB %u times from %s to %s in %ums: %uMB/s\n",
size >> 10, n,
lsdc_domain_to_str(src_domain),
lsdc_domain_to_str(dst_domain),
time, throughput);
}
int lsdc_show_benchmark_copy(struct lsdc_device *ldev, struct drm_printer *p)
{
unsigned int buffer_size = 1920 * 1080 * 4;
unsigned int iteration = 60;
lsdc_benchmark_copy(ldev,
buffer_size,
iteration,
LSDC_GEM_DOMAIN_GTT,
LSDC_GEM_DOMAIN_GTT,
lsdc_copy_gtt_to_gtt_cpu,
p);
lsdc_benchmark_copy(ldev,
buffer_size,
iteration,
LSDC_GEM_DOMAIN_GTT,
LSDC_GEM_DOMAIN_VRAM,
lsdc_copy_gtt_to_vram_cpu,
p);
lsdc_benchmark_copy(ldev,
buffer_size,
iteration,
LSDC_GEM_DOMAIN_VRAM,
LSDC_GEM_DOMAIN_GTT,
lsdc_copy_vram_to_gtt_cpu,
p);
return 0;
}
/* SPDX-License-Identifier: GPL-2.0+ */
/*
* Copyright (C) 2023 Loongson Technology Corporation Limited
*/
#ifndef __LSDC_BENCHMARK_H__
#define __LSDC_BENCHMARK_H__
#include "lsdc_drv.h"
int lsdc_show_benchmark_copy(struct lsdc_device *ldev, struct drm_printer *p);
#endif
This diff is collapsed.
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright (C) 2023 Loongson Technology Corporation Limited
*/
#include <drm/drm_debugfs.h>
#include "lsdc_benchmark.h"
#include "lsdc_drv.h"
#include "lsdc_gem.h"
#include "lsdc_probe.h"
#include "lsdc_ttm.h"
/* device level debugfs */
static int lsdc_identify(struct seq_file *m, void *arg)
{
struct drm_info_node *node = (struct drm_info_node *)m->private;
struct lsdc_device *ldev = (struct lsdc_device *)node->info_ent->data;
const struct loongson_gfx_desc *gfx = to_loongson_gfx(ldev->descp);
u8 impl, rev;
loongson_cpu_get_prid(&impl, &rev);
seq_printf(m, "Running on cpu 0x%x, cpu revision: 0x%x\n",
impl, rev);
seq_printf(m, "Contained in: %s\n", gfx->model);
return 0;
}
static int lsdc_show_mm(struct seq_file *m, void *arg)
{
struct drm_info_node *node = (struct drm_info_node *)m->private;
struct drm_device *ddev = node->minor->dev;
struct drm_printer p = drm_seq_file_printer(m);
drm_mm_print(&ddev->vma_offset_manager->vm_addr_space_mm, &p);
return 0;
}
static int lsdc_show_gfxpll_clock(struct seq_file *m, void *arg)
{
struct drm_info_node *node = (struct drm_info_node *)m->private;
struct lsdc_device *ldev = (struct lsdc_device *)node->info_ent->data;
struct drm_printer printer = drm_seq_file_printer(m);
struct loongson_gfxpll *gfxpll = ldev->gfxpll;
gfxpll->funcs->print(gfxpll, &printer, true);
return 0;
}
static int lsdc_show_benchmark(struct seq_file *m, void *arg)
{
struct drm_info_node *node = (struct drm_info_node *)m->private;
struct lsdc_device *ldev = (struct lsdc_device *)node->info_ent->data;
struct drm_printer printer = drm_seq_file_printer(m);
lsdc_show_benchmark_copy(ldev, &printer);
return 0;
}
static int lsdc_pdev_enable_io_mem(struct seq_file *m, void *arg)
{
struct drm_info_node *node = (struct drm_info_node *)m->private;
struct lsdc_device *ldev = (struct lsdc_device *)node->info_ent->data;
u16 cmd;
pci_read_config_word(ldev->dc, PCI_COMMAND, &cmd);
seq_printf(m, "PCI_COMMAND: 0x%x\n", cmd);
cmd |= PCI_COMMAND_MEMORY | PCI_COMMAND_IO;
pci_write_config_word(ldev->dc, PCI_COMMAND, cmd);
pci_read_config_word(ldev->dc, PCI_COMMAND, &cmd);
seq_printf(m, "PCI_COMMAND: 0x%x\n", cmd);
return 0;
}
static struct drm_info_list lsdc_debugfs_list[] = {
{ "benchmark", lsdc_show_benchmark, 0, NULL },
{ "bos", lsdc_show_buffer_object, 0, NULL },
{ "chips", lsdc_identify, 0, NULL },
{ "clocks", lsdc_show_gfxpll_clock, 0, NULL },
{ "dc_enable", lsdc_pdev_enable_io_mem, 0, NULL },
{ "mm", lsdc_show_mm, 0, NULL },
};
void lsdc_debugfs_init(struct drm_minor *minor)
{
struct drm_device *ddev = minor->dev;
struct lsdc_device *ldev = to_lsdc(ddev);
unsigned int n = ARRAY_SIZE(lsdc_debugfs_list);
unsigned int i;
for (i = 0; i < n; ++i)
lsdc_debugfs_list[i].data = ldev;
drm_debugfs_create_files(lsdc_debugfs_list, n, minor->debugfs_root, minor);
lsdc_ttm_debugfs_init(ldev);
}
This diff is collapsed.
/* SPDX-License-Identifier: GPL-2.0+ */
/*
* Copyright (C) 2023 Loongson Technology Corporation Limited
*/
#ifndef __LSDC_DRV_H__
#define __LSDC_DRV_H__
#include <linux/pci.h>
#include <drm/drm_connector.h>
#include <drm/drm_crtc.h>
#include <drm/drm_device.h>
#include <drm/drm_encoder.h>
#include <drm/drm_file.h>
#include <drm/drm_plane.h>
#include <drm/ttm/ttm_device.h>
#include "lsdc_i2c.h"
#include "lsdc_irq.h"
#include "lsdc_gfxpll.h"
#include "lsdc_output.h"
#include "lsdc_pixpll.h"
#include "lsdc_regs.h"
/* Currently, all Loongson display controllers have two display pipes. */
#define LSDC_NUM_CRTC 2
/*
* LS7A1000/LS7A2000 chipsets function as the south & north bridges of the
* Loongson 3 series processors, they are equipped with on-board video RAM
* typically. While Loongson LS2K series are low cost SoCs which share the
* system RAM as video RAM, they don't has a dedicated VRAM.
*
* There is only a 1:1 mapping of crtcs, encoders and connectors for the DC
*
* display pipe 0 = crtc0 + dvo0 + encoder0 + connector0 + cursor0 + primary0
* display pipe 1 = crtc1 + dvo1 + encoder1 + connectro1 + cursor1 + primary1
*/
enum loongson_chip_id {
CHIP_LS7A1000 = 0,
CHIP_LS7A2000 = 1,
CHIP_LS_LAST,
};
const struct lsdc_desc *
lsdc_device_probe(struct pci_dev *pdev, enum loongson_chip_id chip);
struct lsdc_kms_funcs;
/* DC specific */
struct lsdc_desc {
u32 num_of_crtc;
u32 max_pixel_clk;
u32 max_width;
u32 max_height;
u32 num_of_hw_cursor;
u32 hw_cursor_w;
u32 hw_cursor_h;
u32 pitch_align; /* CRTC DMA alignment constraint */
bool has_vblank_counter; /* 32 bit hw vsync counter */
/* device dependent ops, dc side */
const struct lsdc_kms_funcs *funcs;
};
/* GFX related resources wrangler */
struct loongson_gfx_desc {
struct lsdc_desc dc;
u32 conf_reg_base;
/* GFXPLL shared by the DC, GMC and GPU */
struct {
u32 reg_offset;
u32 reg_size;
} gfxpll;
/* Pixel PLL, per display pipe */
struct {
u32 reg_offset;
u32 reg_size;
} pixpll[LSDC_NUM_CRTC];
enum loongson_chip_id chip_id;
char model[64];
};
static inline const struct loongson_gfx_desc *
to_loongson_gfx(const struct lsdc_desc *dcp)
{
return container_of_const(dcp, struct loongson_gfx_desc, dc);
};
struct lsdc_reg32 {
char *name;
u32 offset;
};
/* crtc hardware related ops */
struct lsdc_crtc;
struct lsdc_crtc_hw_ops {
void (*enable)(struct lsdc_crtc *lcrtc);
void (*disable)(struct lsdc_crtc *lcrtc);
void (*enable_vblank)(struct lsdc_crtc *lcrtc);
void (*disable_vblank)(struct lsdc_crtc *lcrtc);
void (*flip)(struct lsdc_crtc *lcrtc);
void (*clone)(struct lsdc_crtc *lcrtc);
void (*get_scan_pos)(struct lsdc_crtc *lcrtc, int *hpos, int *vpos);
void (*set_mode)(struct lsdc_crtc *lcrtc, const struct drm_display_mode *mode);
void (*soft_reset)(struct lsdc_crtc *lcrtc);
void (*reset)(struct lsdc_crtc *lcrtc);
u32 (*get_vblank_counter)(struct lsdc_crtc *lcrtc);
void (*set_dma_step)(struct lsdc_crtc *lcrtc, enum lsdc_dma_steps step);
};
struct lsdc_crtc {
struct drm_crtc base;
struct lsdc_pixpll pixpll;
struct lsdc_device *ldev;
const struct lsdc_crtc_hw_ops *hw_ops;
const struct lsdc_reg32 *preg;
unsigned int nreg;
struct drm_info_list *p_info_list;
unsigned int n_info_list;
bool has_vblank;
};
/* primary plane hardware related ops */
struct lsdc_primary;
struct lsdc_primary_plane_ops {
void (*update_fb_addr)(struct lsdc_primary *plane, u64 addr);
void (*update_fb_stride)(struct lsdc_primary *plane, u32 stride);
void (*update_fb_format)(struct lsdc_primary *plane,
const struct drm_format_info *format);
};
struct lsdc_primary {
struct drm_plane base;
const struct lsdc_primary_plane_ops *ops;
struct lsdc_device *ldev;
};
/* cursor plane hardware related ops */
struct lsdc_cursor;
struct lsdc_cursor_plane_ops {
void (*update_bo_addr)(struct lsdc_cursor *plane, u64 addr);
void (*update_cfg)(struct lsdc_cursor *plane,
enum lsdc_cursor_size cursor_size,
enum lsdc_cursor_format);
void (*update_position)(struct lsdc_cursor *plane, int x, int y);
};
struct lsdc_cursor {
struct drm_plane base;
const struct lsdc_cursor_plane_ops *ops;
struct lsdc_device *ldev;
};
struct lsdc_output {
struct drm_encoder encoder;
struct drm_connector connector;
};
static inline struct lsdc_output *
connector_to_lsdc_output(struct drm_connector *connector)
{
return container_of(connector, struct lsdc_output, connector);
}
static inline struct lsdc_output *
encoder_to_lsdc_output(struct drm_encoder *encoder)
{
return container_of(encoder, struct lsdc_output, encoder);
}
struct lsdc_display_pipe {
struct lsdc_crtc crtc;
struct lsdc_primary primary;
struct lsdc_cursor cursor;
struct lsdc_output output;
struct lsdc_i2c *li2c;
unsigned int index;
};
static inline struct lsdc_display_pipe *
output_to_display_pipe(struct lsdc_output *output)
{
return container_of(output, struct lsdc_display_pipe, output);
}
struct lsdc_kms_funcs {
irqreturn_t (*irq_handler)(int irq, void *arg);
int (*create_i2c)(struct drm_device *ddev,
struct lsdc_display_pipe *dispipe,
unsigned int index);
int (*output_init)(struct drm_device *ddev,
struct lsdc_display_pipe *dispipe,
struct i2c_adapter *ddc,
unsigned int index);
int (*cursor_plane_init)(struct drm_device *ddev,
struct drm_plane *plane,
unsigned int index);
int (*primary_plane_init)(struct drm_device *ddev,
struct drm_plane *plane,
unsigned int index);
int (*crtc_init)(struct drm_device *ddev,
struct drm_crtc *crtc,
struct drm_plane *primary,
struct drm_plane *cursor,
unsigned int index,
bool has_vblank);
};
static inline struct lsdc_crtc *
to_lsdc_crtc(struct drm_crtc *crtc)
{
return container_of(crtc, struct lsdc_crtc, base);
}
static inline struct lsdc_display_pipe *
crtc_to_display_pipe(struct drm_crtc *crtc)
{
return container_of(crtc, struct lsdc_display_pipe, crtc.base);
}
static inline struct lsdc_primary *
to_lsdc_primary(struct drm_plane *plane)
{
return container_of(plane, struct lsdc_primary, base);
}
static inline struct lsdc_cursor *
to_lsdc_cursor(struct drm_plane *plane)
{
return container_of(plane, struct lsdc_cursor, base);
}
struct lsdc_crtc_state {
struct drm_crtc_state base;
struct lsdc_pixpll_parms pparms;
};
struct lsdc_gem {
/* @mutex: protect objects list */
struct mutex mutex;
struct list_head objects;
};
struct lsdc_device {
struct drm_device base;
struct ttm_device bdev;
/* @descp: features description of the DC variant */
const struct lsdc_desc *descp;
struct pci_dev *dc;
struct pci_dev *gpu;
struct loongson_gfxpll *gfxpll;
/* @reglock: protects concurrent access */
spinlock_t reglock;
void __iomem *reg_base;
resource_size_t vram_base;
resource_size_t vram_size;
resource_size_t gtt_base;
resource_size_t gtt_size;
struct lsdc_display_pipe dispipe[LSDC_NUM_CRTC];
struct lsdc_gem gem;
u32 irq_status;
/* tracking pinned memory */
size_t vram_pinned_size;
size_t gtt_pinned_size;
/* @num_output: count the number of active display pipe */
unsigned int num_output;
};
static inline struct lsdc_device *tdev_to_ldev(struct ttm_device *bdev)
{
return container_of(bdev, struct lsdc_device, bdev);
}
static inline struct lsdc_device *to_lsdc(struct drm_device *ddev)
{
return container_of(ddev, struct lsdc_device, base);
}
static inline struct lsdc_crtc_state *
to_lsdc_crtc_state(struct drm_crtc_state *base)
{
return container_of(base, struct lsdc_crtc_state, base);
}
void lsdc_debugfs_init(struct drm_minor *minor);
int ls7a1000_crtc_init(struct drm_device *ddev,
struct drm_crtc *crtc,
struct drm_plane *primary,
struct drm_plane *cursor,
unsigned int index,
bool no_vblank);
int ls7a2000_crtc_init(struct drm_device *ddev,
struct drm_crtc *crtc,
struct drm_plane *primary,
struct drm_plane *cursor,
unsigned int index,
bool no_vblank);
int lsdc_primary_plane_init(struct drm_device *ddev,
struct drm_plane *plane,
unsigned int index);
int ls7a1000_cursor_plane_init(struct drm_device *ddev,
struct drm_plane *plane,
unsigned int index);
int ls7a2000_cursor_plane_init(struct drm_device *ddev,
struct drm_plane *plane,
unsigned int index);
/* Registers access helpers */
static inline u32 lsdc_rreg32(struct lsdc_device *ldev, u32 offset)
{
return readl(ldev->reg_base + offset);
}
static inline void lsdc_wreg32(struct lsdc_device *ldev, u32 offset, u32 val)
{
writel(val, ldev->reg_base + offset);
}
static inline void lsdc_ureg32_set(struct lsdc_device *ldev,
u32 offset,
u32 mask)
{
void __iomem *addr = ldev->reg_base + offset;
u32 val = readl(addr);
writel(val | mask, addr);
}
static inline void lsdc_ureg32_clr(struct lsdc_device *ldev,
u32 offset,
u32 mask)
{
void __iomem *addr = ldev->reg_base + offset;
u32 val = readl(addr);
writel(val & ~mask, addr);
}
static inline u32 lsdc_pipe_rreg32(struct lsdc_device *ldev,
u32 offset, u32 pipe)
{
return readl(ldev->reg_base + offset + pipe * CRTC_PIPE_OFFSET);
}
static inline void lsdc_pipe_wreg32(struct lsdc_device *ldev,
u32 offset, u32 pipe, u32 val)
{
writel(val, ldev->reg_base + offset + pipe * CRTC_PIPE_OFFSET);
}
#endif
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright (C) 2023 Loongson Technology Corporation Limited
*/
#include <linux/dma-buf.h>
#include <drm/drm_debugfs.h>
#include <drm/drm_file.h>
#include <drm/drm_gem.h>
#include <drm/drm_prime.h>
#include "lsdc_drv.h"
#include "lsdc_gem.h"
#include "lsdc_ttm.h"
static int lsdc_gem_prime_pin(struct drm_gem_object *obj)
{
struct lsdc_bo *lbo = gem_to_lsdc_bo(obj);
int ret;
ret = lsdc_bo_reserve(lbo);
if (unlikely(ret))
return ret;
ret = lsdc_bo_pin(lbo, LSDC_GEM_DOMAIN_GTT, NULL);
if (likely(ret == 0))
lbo->sharing_count++;
lsdc_bo_unreserve(lbo);
return ret;
}
static void lsdc_gem_prime_unpin(struct drm_gem_object *obj)
{
struct lsdc_bo *lbo = gem_to_lsdc_bo(obj);
int ret;
ret = lsdc_bo_reserve(lbo);
if (unlikely(ret))
return;
lsdc_bo_unpin(lbo);
if (lbo->sharing_count)
lbo->sharing_count--;
lsdc_bo_unreserve(lbo);
}
static struct sg_table *lsdc_gem_prime_get_sg_table(struct drm_gem_object *obj)
{
struct ttm_buffer_object *tbo = to_ttm_bo(obj);
struct ttm_tt *tt = tbo->ttm;
if (!tt) {
drm_err(obj->dev, "sharing a buffer without backing memory\n");
return ERR_PTR(-ENOMEM);
}
return drm_prime_pages_to_sg(obj->dev, tt->pages, tt->num_pages);
}
static void lsdc_gem_object_free(struct drm_gem_object *obj)
{
struct ttm_buffer_object *tbo = to_ttm_bo(obj);
if (tbo)
ttm_bo_put(tbo);
}
static int lsdc_gem_object_vmap(struct drm_gem_object *obj, struct iosys_map *map)
{
struct ttm_buffer_object *tbo = to_ttm_bo(obj);
struct lsdc_bo *lbo = to_lsdc_bo(tbo);
int ret;
if (lbo->vmap_count > 0) {
++lbo->vmap_count;
goto out;
}
ret = lsdc_bo_pin(lbo, 0, NULL);
if (unlikely(ret)) {
drm_err(obj->dev, "pin %p for vmap failed\n", lbo);
return ret;
}
ret = ttm_bo_vmap(tbo, &lbo->map);
if (ret) {
drm_err(obj->dev, "ttm bo vmap failed\n");
lsdc_bo_unpin(lbo);
return ret;
}
lbo->vmap_count = 1;
out:
*map = lbo->map;
return 0;
}
static void lsdc_gem_object_vunmap(struct drm_gem_object *obj, struct iosys_map *map)
{
struct ttm_buffer_object *tbo = to_ttm_bo(obj);
struct lsdc_bo *lbo = to_lsdc_bo(tbo);
if (unlikely(!lbo->vmap_count)) {
drm_warn(obj->dev, "%p is not mapped\n", lbo);
return;
}
--lbo->vmap_count;
if (lbo->vmap_count == 0) {
ttm_bo_vunmap(tbo, &lbo->map);
lsdc_bo_unpin(lbo);
}
}
static int lsdc_gem_object_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
{
struct ttm_buffer_object *tbo = to_ttm_bo(obj);
int ret;
ret = ttm_bo_mmap_obj(vma, tbo);
if (unlikely(ret)) {
drm_warn(obj->dev, "mmap %p failed\n", tbo);
return ret;
}
drm_gem_object_put(obj);
return 0;
}
static const struct drm_gem_object_funcs lsdc_gem_object_funcs = {
.free = lsdc_gem_object_free,
.export = drm_gem_prime_export,
.pin = lsdc_gem_prime_pin,
.unpin = lsdc_gem_prime_unpin,
.get_sg_table = lsdc_gem_prime_get_sg_table,
.vmap = lsdc_gem_object_vmap,
.vunmap = lsdc_gem_object_vunmap,
.mmap = lsdc_gem_object_mmap,
};
struct drm_gem_object *lsdc_gem_object_create(struct drm_device *ddev,
u32 domain,
size_t size,
bool kerenl,
struct sg_table *sg,
struct dma_resv *resv)
{
struct lsdc_device *ldev = to_lsdc(ddev);
struct drm_gem_object *gobj;
struct lsdc_bo *lbo;
int ret;
lbo = lsdc_bo_create(ddev, domain, size, kerenl, sg, resv);
if (IS_ERR(lbo)) {
ret = PTR_ERR(lbo);
return ERR_PTR(ret);
}
if (!sg) {
/* VRAM is filled with random data */
lsdc_bo_clear(lbo);
}
gobj = &lbo->tbo.base;
gobj->funcs = &lsdc_gem_object_funcs;
/* tracking the BOs we created */
mutex_lock(&ldev->gem.mutex);
list_add_tail(&lbo->list, &ldev->gem.objects);
mutex_unlock(&ldev->gem.mutex);
return gobj;
}
struct drm_gem_object *
lsdc_prime_import_sg_table(struct drm_device *ddev,
struct dma_buf_attachment *attach,
struct sg_table *sg)
{
struct dma_resv *resv = attach->dmabuf->resv;
u64 size = attach->dmabuf->size;
struct drm_gem_object *gobj;
struct lsdc_bo *lbo;
dma_resv_lock(resv, NULL);
gobj = lsdc_gem_object_create(ddev, LSDC_GEM_DOMAIN_GTT, size, false,
sg, resv);
dma_resv_unlock(resv);
if (IS_ERR(gobj)) {
drm_err(ddev, "Failed to import sg table\n");
return gobj;
}
lbo = gem_to_lsdc_bo(gobj);
lbo->sharing_count = 1;
return gobj;
}
int lsdc_dumb_create(struct drm_file *file, struct drm_device *ddev,
struct drm_mode_create_dumb *args)
{
struct lsdc_device *ldev = to_lsdc(ddev);
const struct lsdc_desc *descp = ldev->descp;
u32 domain = LSDC_GEM_DOMAIN_VRAM;
struct drm_gem_object *gobj;
size_t size;
u32 pitch;
u32 handle;
int ret;
if (!args->width || !args->height)
return -EINVAL;
if (args->bpp != 32 && args->bpp != 16)
return -EINVAL;
pitch = args->width * args->bpp / 8;
pitch = ALIGN(pitch, descp->pitch_align);
size = pitch * args->height;
size = ALIGN(size, PAGE_SIZE);
/* Maximum single bo size allowed is the half vram size available */
if (size > ldev->vram_size / 2) {
drm_err(ddev, "Requesting(%zuMiB) failed\n", size >> 20);
return -ENOMEM;
}
gobj = lsdc_gem_object_create(ddev, domain, size, false, NULL, NULL);
if (IS_ERR(gobj)) {
drm_err(ddev, "Failed to create gem object\n");
return PTR_ERR(gobj);
}
ret = drm_gem_handle_create(file, gobj, &handle);
/* drop reference from allocate, handle holds it now */
drm_gem_object_put(gobj);
if (ret)
return ret;
args->pitch = pitch;
args->size = size;
args->handle = handle;
return 0;
}
int lsdc_dumb_map_offset(struct drm_file *filp, struct drm_device *ddev,
u32 handle, uint64_t *offset)
{
struct drm_gem_object *gobj;
gobj = drm_gem_object_lookup(filp, handle);
if (!gobj)
return -ENOENT;
*offset = drm_vma_node_offset_addr(&gobj->vma_node);
drm_gem_object_put(gobj);
return 0;
}
void lsdc_gem_init(struct drm_device *ddev)
{
struct lsdc_device *ldev = to_lsdc(ddev);
mutex_init(&ldev->gem.mutex);
INIT_LIST_HEAD(&ldev->gem.objects);
}
int lsdc_show_buffer_object(struct seq_file *m, void *arg)
{
struct drm_info_node *node = (struct drm_info_node *)m->private;
struct drm_device *ddev = node->minor->dev;
struct lsdc_device *ldev = to_lsdc(ddev);
struct lsdc_bo *lbo;
unsigned int i;
mutex_lock(&ldev->gem.mutex);
i = 0;
list_for_each_entry(lbo, &ldev->gem.objects, list) {
struct ttm_buffer_object *tbo = &lbo->tbo;
struct ttm_resource *resource = tbo->resource;
seq_printf(m, "bo[%04u][%p]: size: %8zuKiB %s offset: %8llx\n",
i, lbo, lsdc_bo_size(lbo) >> 10,
lsdc_mem_type_to_str(resource->mem_type),
lsdc_bo_gpu_offset(lbo));
i++;
}
mutex_unlock(&ldev->gem.mutex);
seq_printf(m, "Pinned BO size: VRAM: %zuKiB, GTT: %zu KiB\n",
ldev->vram_pinned_size >> 10, ldev->gtt_pinned_size >> 10);
return 0;
}
/* SPDX-License-Identifier: GPL-2.0+ */
/*
* Copyright (C) 2023 Loongson Technology Corporation Limited
*/
#ifndef __LSDC_GEM_H__
#define __LSDC_GEM_H__
#include <drm/drm_device.h>
#include <drm/drm_gem.h>
struct drm_gem_object *
lsdc_prime_import_sg_table(struct drm_device *ddev,
struct dma_buf_attachment *attach,
struct sg_table *sg);
int lsdc_dumb_map_offset(struct drm_file *file,
struct drm_device *dev,
u32 handle,
uint64_t *offset);
int lsdc_dumb_create(struct drm_file *file,
struct drm_device *ddev,
struct drm_mode_create_dumb *args);
void lsdc_gem_init(struct drm_device *ddev);
int lsdc_show_buffer_object(struct seq_file *m, void *arg);
struct drm_gem_object *
lsdc_gem_object_create(struct drm_device *ddev,
u32 domain,
size_t size,
bool kerenl,
struct sg_table *sg,
struct dma_resv *resv);
#endif
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright (C) 2023 Loongson Technology Corporation Limited
*/
#include <linux/delay.h>
#include <drm/drm_file.h>
#include <drm/drm_managed.h>
#include <drm/drm_print.h>
#include "lsdc_drv.h"
/*
* GFX PLL is the PLL used by DC, GMC and GPU, the structure of the GFX PLL
* may suffer from change across chip variants.
*
*
* +-------------+ sel_out_dc
* +----| / div_out_0 | _____/ _____ DC
* | +-------------+
* refclk +---------+ +-------+ | +-------------+ sel_out_gmc
* ---+---> | div_ref | ---> | loopc | --+--> | / div_out_1 | _____/ _____ GMC
* | +---------+ +-------+ | +-------------+
* | / * | +-------------+ sel_out_gpu
* | +----| / div_out_2 | _____/ _____ GPU
* | +-------------+
* | ^
* | |
* +--------------------------- bypass ----------------------+
*/
struct loongson_gfxpll_bitmap {
/* Byte 0 ~ Byte 3 */
unsigned div_out_dc : 7; /* 6 : 0 DC output clock divider */
unsigned div_out_gmc : 7; /* 13 : 7 GMC output clock divider */
unsigned div_out_gpu : 7; /* 20 : 14 GPU output clock divider */
unsigned loopc : 9; /* 29 : 21 clock multiplier */
unsigned _reserved_1_ : 2; /* 31 : 30 */
/* Byte 4 ~ Byte 7 */
unsigned div_ref : 7; /* 38 : 32 Input clock divider */
unsigned locked : 1; /* 39 PLL locked indicator */
unsigned sel_out_dc : 1; /* 40 dc output clk enable */
unsigned sel_out_gmc : 1; /* 41 gmc output clk enable */
unsigned sel_out_gpu : 1; /* 42 gpu output clk enable */
unsigned set_param : 1; /* 43 Trigger the update */
unsigned bypass : 1; /* 44 */
unsigned powerdown : 1; /* 45 */
unsigned _reserved_2_ : 18; /* 46 : 63 no use */
};
union loongson_gfxpll_reg_bitmap {
struct loongson_gfxpll_bitmap bitmap;
u32 w[2];
u64 d;
};
static void __gfxpll_rreg(struct loongson_gfxpll *this,
union loongson_gfxpll_reg_bitmap *reg)
{
#if defined(CONFIG_64BIT)
reg->d = readq(this->mmio);
#else
reg->w[0] = readl(this->mmio);
reg->w[1] = readl(this->mmio + 4);
#endif
}
/* Update new parameters to the hardware */
static int loongson_gfxpll_update(struct loongson_gfxpll * const this,
struct loongson_gfxpll_parms const *pin)
{
/* None, TODO */
return 0;
}
static void loongson_gfxpll_get_rates(struct loongson_gfxpll * const this,
unsigned int *dc,
unsigned int *gmc,
unsigned int *gpu)
{
struct loongson_gfxpll_parms *pparms = &this->parms;
union loongson_gfxpll_reg_bitmap gfxpll_reg;
unsigned int pre_output;
unsigned int dc_mhz;
unsigned int gmc_mhz;
unsigned int gpu_mhz;
__gfxpll_rreg(this, &gfxpll_reg);
pparms->div_ref = gfxpll_reg.bitmap.div_ref;
pparms->loopc = gfxpll_reg.bitmap.loopc;
pparms->div_out_dc = gfxpll_reg.bitmap.div_out_dc;
pparms->div_out_gmc = gfxpll_reg.bitmap.div_out_gmc;
pparms->div_out_gpu = gfxpll_reg.bitmap.div_out_gpu;
pre_output = pparms->ref_clock / pparms->div_ref * pparms->loopc;
dc_mhz = pre_output / pparms->div_out_dc / 1000;
gmc_mhz = pre_output / pparms->div_out_gmc / 1000;
gpu_mhz = pre_output / pparms->div_out_gpu / 1000;
if (dc)
*dc = dc_mhz;
if (gmc)
*gmc = gmc_mhz;
if (gpu)
*gpu = gpu_mhz;
}
static void loongson_gfxpll_print(struct loongson_gfxpll * const this,
struct drm_printer *p,
bool verbose)
{
struct loongson_gfxpll_parms *parms = &this->parms;
unsigned int dc, gmc, gpu;
if (verbose) {
drm_printf(p, "reference clock: %u\n", parms->ref_clock);
drm_printf(p, "div_ref = %u\n", parms->div_ref);
drm_printf(p, "loopc = %u\n", parms->loopc);
drm_printf(p, "div_out_dc = %u\n", parms->div_out_dc);
drm_printf(p, "div_out_gmc = %u\n", parms->div_out_gmc);
drm_printf(p, "div_out_gpu = %u\n", parms->div_out_gpu);
}
this->funcs->get_rates(this, &dc, &gmc, &gpu);
drm_printf(p, "dc: %uMHz, gmc: %uMHz, gpu: %uMHz\n", dc, gmc, gpu);
}
/* GFX (DC, GPU, GMC) PLL initialization and destroy function */
static void loongson_gfxpll_fini(struct drm_device *ddev, void *data)
{
struct loongson_gfxpll *this = (struct loongson_gfxpll *)data;
iounmap(this->mmio);
kfree(this);
}
static int loongson_gfxpll_init(struct loongson_gfxpll * const this)
{
struct loongson_gfxpll_parms *pparms = &this->parms;
struct drm_printer printer = drm_info_printer(this->ddev->dev);
pparms->ref_clock = LSDC_PLL_REF_CLK_KHZ;
this->mmio = ioremap(this->reg_base, this->reg_size);
if (IS_ERR_OR_NULL(this->mmio))
return -ENOMEM;
this->funcs->print(this, &printer, false);
return 0;
}
static const struct loongson_gfxpll_funcs lsdc_gmc_gpu_funcs = {
.init = loongson_gfxpll_init,
.update = loongson_gfxpll_update,
.get_rates = loongson_gfxpll_get_rates,
.print = loongson_gfxpll_print,
};
int loongson_gfxpll_create(struct drm_device *ddev,
struct loongson_gfxpll **ppout)
{
struct lsdc_device *ldev = to_lsdc(ddev);
const struct loongson_gfx_desc *gfx = to_loongson_gfx(ldev->descp);
struct loongson_gfxpll *this;
int ret;
this = kzalloc(sizeof(*this), GFP_KERNEL);
if (IS_ERR_OR_NULL(this))
return -ENOMEM;
this->ddev = ddev;
this->reg_size = gfx->gfxpll.reg_size;
this->reg_base = gfx->conf_reg_base + gfx->gfxpll.reg_offset;
this->funcs = &lsdc_gmc_gpu_funcs;
ret = this->funcs->init(this);
if (unlikely(ret)) {
kfree(this);
return ret;
}
*ppout = this;
return drmm_add_action_or_reset(ddev, loongson_gfxpll_fini, this);
}
/* SPDX-License-Identifier: GPL-2.0+ */
/*
* Copyright (C) 2023 Loongson Technology Corporation Limited
*/
#ifndef __LSDC_GFXPLL_H__
#define __LSDC_GFXPLL_H__
#include <drm/drm_device.h>
struct loongson_gfxpll;
struct loongson_gfxpll_parms {
unsigned int ref_clock;
unsigned int div_ref;
unsigned int loopc;
unsigned int div_out_dc;
unsigned int div_out_gmc;
unsigned int div_out_gpu;
};
struct loongson_gfxpll_funcs {
int (*init)(struct loongson_gfxpll * const this);
int (*update)(struct loongson_gfxpll * const this,
struct loongson_gfxpll_parms const *pin);
void (*get_rates)(struct loongson_gfxpll * const this,
unsigned int *dc, unsigned int *gmc, unsigned int *gpu);
void (*print)(struct loongson_gfxpll * const this,
struct drm_printer *printer, bool verbose);
};
struct loongson_gfxpll {
struct drm_device *ddev;
void __iomem *mmio;
/* PLL register offset */
u32 reg_base;
/* PLL register size in bytes */
u32 reg_size;
const struct loongson_gfxpll_funcs *funcs;
struct loongson_gfxpll_parms parms;
};
int loongson_gfxpll_create(struct drm_device *ddev,
struct loongson_gfxpll **ppout);
#endif
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright (C) 2023 Loongson Technology Corporation Limited
*/
#include <drm/drm_managed.h>
#include "lsdc_drv.h"
#include "lsdc_output.h"
/*
* __lsdc_gpio_i2c_set - set the state of a gpio pin indicated by mask
* @mask: gpio pin mask
* @state: "0" for low, "1" for high
*/
static void __lsdc_gpio_i2c_set(struct lsdc_i2c * const li2c, int mask, int state)
{
struct lsdc_device *ldev = to_lsdc(li2c->ddev);
unsigned long flags;
u8 val;
spin_lock_irqsave(&ldev->reglock, flags);
if (state) {
/*
* Setting this pin as input directly, write 1 for input.
* The external pull-up resistor will pull the level up
*/
val = readb(li2c->dir_reg);
val |= mask;
writeb(val, li2c->dir_reg);
} else {
/* First set this pin as output, write 0 for output */
val = readb(li2c->dir_reg);
val &= ~mask;
writeb(val, li2c->dir_reg);
/* Then, make this pin output 0 */
val = readb(li2c->dat_reg);
val &= ~mask;
writeb(val, li2c->dat_reg);
}
spin_unlock_irqrestore(&ldev->reglock, flags);
}
/*
* __lsdc_gpio_i2c_get - read value back from the gpio pin indicated by mask
* @mask: gpio pin mask
* return "0" for low, "1" for high
*/
static int __lsdc_gpio_i2c_get(struct lsdc_i2c * const li2c, int mask)
{
struct lsdc_device *ldev = to_lsdc(li2c->ddev);
unsigned long flags;
u8 val;
spin_lock_irqsave(&ldev->reglock, flags);
/* First set this pin as input */
val = readb(li2c->dir_reg);
val |= mask;
writeb(val, li2c->dir_reg);
/* Then get level state from this pin */
val = readb(li2c->dat_reg);
spin_unlock_irqrestore(&ldev->reglock, flags);
return (val & mask) ? 1 : 0;
}
static void lsdc_gpio_i2c_set_sda(void *i2c, int state)
{
struct lsdc_i2c * const li2c = (struct lsdc_i2c *)i2c;
/* set state on the li2c->sda pin */
return __lsdc_gpio_i2c_set(li2c, li2c->sda, state);
}
static void lsdc_gpio_i2c_set_scl(void *i2c, int state)
{
struct lsdc_i2c * const li2c = (struct lsdc_i2c *)i2c;
/* set state on the li2c->scl pin */
return __lsdc_gpio_i2c_set(li2c, li2c->scl, state);
}
static int lsdc_gpio_i2c_get_sda(void *i2c)
{
struct lsdc_i2c * const li2c = (struct lsdc_i2c *)i2c;
/* read value from the li2c->sda pin */
return __lsdc_gpio_i2c_get(li2c, li2c->sda);
}
static int lsdc_gpio_i2c_get_scl(void *i2c)
{
struct lsdc_i2c * const li2c = (struct lsdc_i2c *)i2c;
/* read the value from the li2c->scl pin */
return __lsdc_gpio_i2c_get(li2c, li2c->scl);
}
static void lsdc_destroy_i2c(struct drm_device *ddev, void *data)
{
struct lsdc_i2c *li2c = (struct lsdc_i2c *)data;
if (li2c) {
i2c_del_adapter(&li2c->adapter);
kfree(li2c);
}
}
/*
* The DC in ls7a1000/ls7a2000/ls2k2000 has builtin gpio hardware
*
* @reg_base: gpio reg base
* @index: output channel index, 0 for PIPE0, 1 for PIPE1
*/
int lsdc_create_i2c_chan(struct drm_device *ddev,
struct lsdc_display_pipe *dispipe,
unsigned int index)
{
struct lsdc_device *ldev = to_lsdc(ddev);
struct i2c_adapter *adapter;
struct lsdc_i2c *li2c;
int ret;
li2c = kzalloc(sizeof(*li2c), GFP_KERNEL);
if (!li2c)
return -ENOMEM;
dispipe->li2c = li2c;
if (index == 0) {
li2c->sda = 0x01; /* pin 0 */
li2c->scl = 0x02; /* pin 1 */
} else if (index == 1) {
li2c->sda = 0x04; /* pin 2 */
li2c->scl = 0x08; /* pin 3 */
} else {
return -ENOENT;
}
li2c->ddev = ddev;
li2c->dir_reg = ldev->reg_base + LS7A_DC_GPIO_DIR_REG;
li2c->dat_reg = ldev->reg_base + LS7A_DC_GPIO_DAT_REG;
li2c->bit.setsda = lsdc_gpio_i2c_set_sda;
li2c->bit.setscl = lsdc_gpio_i2c_set_scl;
li2c->bit.getsda = lsdc_gpio_i2c_get_sda;
li2c->bit.getscl = lsdc_gpio_i2c_get_scl;
li2c->bit.udelay = 5;
li2c->bit.timeout = usecs_to_jiffies(2200);
li2c->bit.data = li2c;
adapter = &li2c->adapter;
adapter->algo_data = &li2c->bit;
adapter->owner = THIS_MODULE;
adapter->class = I2C_CLASS_DDC;
adapter->dev.parent = ddev->dev;
adapter->nr = -1;
snprintf(adapter->name, sizeof(adapter->name), "lsdc-i2c%u", index);
i2c_set_adapdata(adapter, li2c);
ret = i2c_bit_add_bus(adapter);
if (ret) {
kfree(li2c);
return ret;
}
ret = drmm_add_action_or_reset(ddev, lsdc_destroy_i2c, li2c);
if (ret)
return ret;
drm_info(ddev, "%s(sda pin mask=%u, scl pin mask=%u) created\n",
adapter->name, li2c->sda, li2c->scl);
return 0;
}
/* SPDX-License-Identifier: GPL-2.0+ */
/*
* Copyright (C) 2023 Loongson Technology Corporation Limited
*/
#ifndef __LSDC_I2C_H__
#define __LSDC_I2C_H__
#include <linux/i2c.h>
#include <linux/i2c-algo-bit.h>
struct lsdc_i2c {
struct i2c_adapter adapter;
struct i2c_algo_bit_data bit;
struct drm_device *ddev;
void __iomem *dir_reg;
void __iomem *dat_reg;
/* pin bit mask */
u8 sda;
u8 scl;
};
struct lsdc_display_pipe;
int lsdc_create_i2c_chan(struct drm_device *ddev,
struct lsdc_display_pipe *dispipe,
unsigned int index);
#endif
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright (C) 2023 Loongson Technology Corporation Limited
*/
#include <drm/drm_vblank.h>
#include "lsdc_irq.h"
/*
* For the DC in LS7A2000, clearing interrupt status is achieved by
* write "1" to LSDC_INT_REG.
*
* For the DC in LS7A1000, clear interrupt status is achieved by write "0"
* to LSDC_INT_REG.
*
* Two different hardware engineers modify it as their will.
*/
irqreturn_t ls7a2000_dc_irq_handler(int irq, void *arg)
{
struct drm_device *ddev = arg;
struct lsdc_device *ldev = to_lsdc(ddev);
u32 val;
/* Read the interrupt status */
val = lsdc_rreg32(ldev, LSDC_INT_REG);
if ((val & INT_STATUS_MASK) == 0) {
drm_warn(ddev, "no interrupt occurs\n");
return IRQ_NONE;
}
ldev->irq_status = val;
/* write "1" to clear the interrupt status */
lsdc_wreg32(ldev, LSDC_INT_REG, val);
if (ldev->irq_status & INT_CRTC0_VSYNC)
drm_handle_vblank(ddev, 0);
if (ldev->irq_status & INT_CRTC1_VSYNC)
drm_handle_vblank(ddev, 1);
return IRQ_HANDLED;
}
/* For the DC in LS7A1000 and LS2K1000 */
irqreturn_t ls7a1000_dc_irq_handler(int irq, void *arg)
{
struct drm_device *ddev = arg;
struct lsdc_device *ldev = to_lsdc(ddev);
u32 val;
/* Read the interrupt status */
val = lsdc_rreg32(ldev, LSDC_INT_REG);
if ((val & INT_STATUS_MASK) == 0) {
drm_warn(ddev, "no interrupt occurs\n");
return IRQ_NONE;
}
ldev->irq_status = val;
/* write "0" to clear the interrupt status */
val &= ~(INT_CRTC0_VSYNC | INT_CRTC1_VSYNC);
lsdc_wreg32(ldev, LSDC_INT_REG, val);
if (ldev->irq_status & INT_CRTC0_VSYNC)
drm_handle_vblank(ddev, 0);
if (ldev->irq_status & INT_CRTC1_VSYNC)
drm_handle_vblank(ddev, 1);
return IRQ_HANDLED;
}
/* SPDX-License-Identifier: GPL-2.0+ */
/*
* Copyright (C) 2023 Loongson Technology Corporation Limited
*/
#ifndef __LSDC_IRQ_H__
#define __LSDC_IRQ_H__
#include <linux/irqreturn.h>
#include "lsdc_drv.h"
irqreturn_t ls7a1000_dc_irq_handler(int irq, void *arg);
irqreturn_t ls7a2000_dc_irq_handler(int irq, void *arg);
#endif
/* SPDX-License-Identifier: GPL-2.0+ */
/*
* Copyright (C) 2023 Loongson Technology Corporation Limited
*/
#ifndef __LSDC_OUTPUT_H__
#define __LSDC_OUTPUT_H__
#include "lsdc_drv.h"
int ls7a1000_output_init(struct drm_device *ddev,
struct lsdc_display_pipe *dispipe,
struct i2c_adapter *ddc,
unsigned int index);
int ls7a2000_output_init(struct drm_device *ldev,
struct lsdc_display_pipe *dispipe,
struct i2c_adapter *ddc,
unsigned int index);
#endif
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2023 Loongson Technology Corporation Limited
*/
#include <drm/drm_atomic_helper.h>
#include <drm/drm_edid.h>
#include <drm/drm_probe_helper.h>
#include "lsdc_drv.h"
#include "lsdc_output.h"
/*
* The display controller in the LS7A1000 exports two DVO interfaces, thus
* external encoder is required, except connected to the DPI panel directly.
*
* ___________________ _________
* | -------| | |
* | CRTC0 --> | DVO0 ----> Encoder0 ---> Connector0 ---> | Display |
* | _ _ -------| ^ ^ |_________|
* | | | | | +------+ | | |
* | |_| |_| | i2c6 | <--------+-------------+
* | +------+ |
* | |
* | DC in LS7A1000 |
* | |
* | _ _ +------+ |
* | | | | | | i2c7 | <--------+-------------+
* | |_| |_| +------+ | | | _________
* | -------| | | | |
* | CRTC1 --> | DVO1 ----> Encoder1 ---> Connector1 ---> | Panel |
* | -------| |_________|
* |___________________|
*
* Currently, we assume the external encoders connected to the DVO are
* transparent. Loongson's DVO interface can directly drive RGB888 panels.
*
* TODO: Add support for non-transparent encoders
*/
static int ls7a1000_dpi_connector_get_modes(struct drm_connector *conn)
{
unsigned int num = 0;
struct edid *edid;
if (conn->ddc) {
edid = drm_get_edid(conn, conn->ddc);
if (edid) {
drm_connector_update_edid_property(conn, edid);
num = drm_add_edid_modes(conn, edid);
kfree(edid);
}
return num;
}
num = drm_add_modes_noedid(conn, 1920, 1200);
drm_set_preferred_mode(conn, 1024, 768);
return num;
}
static struct drm_encoder *
ls7a1000_dpi_connector_get_best_encoder(struct drm_connector *connector,
struct drm_atomic_state *state)
{
struct lsdc_output *output = connector_to_lsdc_output(connector);
return &output->encoder;
}
static const struct drm_connector_helper_funcs
ls7a1000_dpi_connector_helpers = {
.atomic_best_encoder = ls7a1000_dpi_connector_get_best_encoder,
.get_modes = ls7a1000_dpi_connector_get_modes,
};
static enum drm_connector_status
ls7a1000_dpi_connector_detect(struct drm_connector *connector, bool force)
{
struct i2c_adapter *ddc = connector->ddc;
if (ddc) {
if (drm_probe_ddc(ddc))
return connector_status_connected;
return connector_status_disconnected;
}
return connector_status_unknown;
}
static const struct drm_connector_funcs ls7a1000_dpi_connector_funcs = {
.detect = ls7a1000_dpi_connector_detect,
.fill_modes = drm_helper_probe_single_connector_modes,
.destroy = drm_connector_cleanup,
.reset = drm_atomic_helper_connector_reset,
.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
.atomic_destroy_state = drm_atomic_helper_connector_destroy_state
};
static void ls7a1000_pipe0_encoder_reset(struct drm_encoder *encoder)
{
struct drm_device *ddev = encoder->dev;
struct lsdc_device *ldev = to_lsdc(ddev);
/*
* We need this for S3 support, screen will not lightup if don't set
* this register correctly.
*/
lsdc_wreg32(ldev, LSDC_CRTC0_DVO_CONF_REG,
PHY_CLOCK_POL | PHY_CLOCK_EN | PHY_DATA_EN);
}
static void ls7a1000_pipe1_encoder_reset(struct drm_encoder *encoder)
{
struct drm_device *ddev = encoder->dev;
struct lsdc_device *ldev = to_lsdc(ddev);
/*
* We need this for S3 support, screen will not lightup if don't set
* this register correctly.
*/
/* DVO */
lsdc_wreg32(ldev, LSDC_CRTC1_DVO_CONF_REG,
BIT(31) | PHY_CLOCK_POL | PHY_CLOCK_EN | PHY_DATA_EN);
}
static const struct drm_encoder_funcs ls7a1000_encoder_funcs[2] = {
{
.reset = ls7a1000_pipe0_encoder_reset,
.destroy = drm_encoder_cleanup,
},
{
.reset = ls7a1000_pipe1_encoder_reset,
.destroy = drm_encoder_cleanup,
},
};
int ls7a1000_output_init(struct drm_device *ddev,
struct lsdc_display_pipe *dispipe,
struct i2c_adapter *ddc,
unsigned int index)
{
struct lsdc_output *output = &dispipe->output;
struct drm_encoder *encoder = &output->encoder;
struct drm_connector *connector = &output->connector;
int ret;
ret = drm_encoder_init(ddev, encoder, &ls7a1000_encoder_funcs[index],
DRM_MODE_ENCODER_TMDS, "encoder-%u", index);
if (ret)
return ret;
encoder->possible_crtcs = BIT(index);
ret = drm_connector_init_with_ddc(ddev, connector,
&ls7a1000_dpi_connector_funcs,
DRM_MODE_CONNECTOR_DPI, ddc);
if (ret)
return ret;
drm_info(ddev, "display pipe-%u has a DVO\n", index);
drm_connector_helper_add(connector, &ls7a1000_dpi_connector_helpers);
drm_connector_attach_encoder(connector, encoder);
connector->polled = DRM_CONNECTOR_POLL_CONNECT |
DRM_CONNECTOR_POLL_DISCONNECT;
connector->interlace_allowed = 0;
connector->doublescan_allowed = 0;
return 0;
}
This diff is collapsed.
This diff is collapsed.
/* SPDX-License-Identifier: GPL-2.0+ */
/*
* Copyright (C) 2023 Loongson Technology Corporation Limited
*/
#ifndef __LSDC_PIXPLL_H__
#define __LSDC_PIXPLL_H__
#include <drm/drm_device.h>
/*
* Loongson Pixel PLL hardware structure
*
* refclk: reference frequency, 100 MHz from external oscillator
* outclk: output frequency desired.
*
*
* L1 Fref Fvco L2
* refclk +-----------+ +------------------+ +---------+ outclk
* ---+---> | Prescaler | ---> | Clock Multiplier | ---> | divider | -------->
* | +-----------+ +------------------+ +---------+ ^
* | ^ ^ ^ |
* | | | | |
* | | | | |
* | div_ref loopc div_out |
* | |
* +---- bypass (bypass above software configurable clock if set) ----+
*
* outclk = refclk / div_ref * loopc / div_out;
*
* sel_out: PLL clock output selector(enable).
*
* If sel_out == 1, then enable output clock (turn On);
* If sel_out == 0, then disable output clock (turn Off);
*
* PLL working requirements:
*
* 1) 20 MHz <= refclk / div_ref <= 40Mhz
* 2) 1.2 GHz <= refclk /div_out * loopc <= 3.2 Ghz
*/
struct lsdc_pixpll_parms {
unsigned int ref_clock;
unsigned int div_ref;
unsigned int loopc;
unsigned int div_out;
};
struct lsdc_pixpll;
struct lsdc_pixpll_funcs {
int (*setup)(struct lsdc_pixpll * const this);
int (*compute)(struct lsdc_pixpll * const this,
unsigned int clock,
struct lsdc_pixpll_parms *pout);
int (*update)(struct lsdc_pixpll * const this,
struct lsdc_pixpll_parms const *pin);
unsigned int (*get_rate)(struct lsdc_pixpll * const this);
void (*print)(struct lsdc_pixpll * const this,
struct drm_printer *printer);
};
struct lsdc_pixpll {
const struct lsdc_pixpll_funcs *funcs;
struct drm_device *ddev;
/* PLL register offset */
u32 reg_base;
/* PLL register size in bytes */
u32 reg_size;
void __iomem *mmio;
struct lsdc_pixpll_parms *priv;
};
int lsdc_pixpll_init(struct lsdc_pixpll * const this,
struct drm_device *ddev,
unsigned int index);
#endif
This diff is collapsed.
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright (C) 2023 Loongson Technology Corporation Limited
*/
#include "lsdc_drv.h"
#include "lsdc_probe.h"
/*
* Processor ID (implementation) values for bits 15:8 of the PRID register.
*/
#define LOONGSON_CPU_IMP_MASK 0xff00
#define LOONGSON_CPU_IMP_SHIFT 8
#define LOONGARCH_CPU_IMP_LS2K1000 0xa0
#define LOONGARCH_CPU_IMP_LS2K2000 0xb0
#define LOONGARCH_CPU_IMP_LS3A5000 0xc0
#define LOONGSON_CPU_MIPS_IMP_LS2K 0x61 /* Loongson 2K Mips series SoC */
/*
* Particular Revision values for bits 7:0 of the PRID register.
*/
#define LOONGSON_CPU_REV_MASK 0x00ff
#define LOONGARCH_CPUCFG_PRID_REG 0x0
/*
* We can achieve fine-grained control with the information about the host.
*/
unsigned int loongson_cpu_get_prid(u8 *imp, u8 *rev)
{
unsigned int prid = 0;
#if defined(__loongarch__)
__asm__ volatile("cpucfg %0, %1\n\t"
: "=&r"(prid)
: "r"(LOONGARCH_CPUCFG_PRID_REG)
);
#endif
#if defined(__mips__)
__asm__ volatile("mfc0\t%0, $15\n\t"
: "=r" (prid)
);
#endif
if (imp)
*imp = (prid & LOONGSON_CPU_IMP_MASK) >> LOONGSON_CPU_IMP_SHIFT;
if (rev)
*rev = prid & LOONGSON_CPU_REV_MASK;
return prid;
}
/* SPDX-License-Identifier: GPL-2.0+ */
/*
* Copyright (C) 2023 Loongson Technology Corporation Limited
*/
#ifndef __LSDC_PROBE_H__
#define __LSDC_PROBE_H__
/* Helpers for chip detection */
unsigned int loongson_cpu_get_prid(u8 *impl, u8 *rev);
#endif
This diff is collapsed.
This diff is collapsed.
/* SPDX-License-Identifier: GPL-2.0+ */
/*
* Copyright (C) 2023 Loongson Technology Corporation Limited
*/
#ifndef __LSDC_TTM_H__
#define __LSDC_TTM_H__
#include <linux/container_of.h>
#include <linux/iosys-map.h>
#include <linux/list.h>
#include <drm/drm_gem.h>
#include <drm/ttm/ttm_bo.h>
#include <drm/ttm/ttm_placement.h>
#include <drm/ttm/ttm_range_manager.h>
#include <drm/ttm/ttm_tt.h>
#define LSDC_GEM_DOMAIN_SYSTEM 0x1
#define LSDC_GEM_DOMAIN_GTT 0x2
#define LSDC_GEM_DOMAIN_VRAM 0x4
struct lsdc_bo {
struct ttm_buffer_object tbo;
/* Protected by gem.mutex */
struct list_head list;
struct iosys_map map;
unsigned int vmap_count;
/* cross device driver sharing reference count */
unsigned int sharing_count;
struct ttm_bo_kmap_obj kmap;
void *kptr;
bool is_iomem;
size_t size;
u32 initial_domain;
struct ttm_placement placement;
struct ttm_place placements[4];
};
static inline struct ttm_buffer_object *to_ttm_bo(struct drm_gem_object *gem)
{
return container_of(gem, struct ttm_buffer_object, base);
}
static inline struct lsdc_bo *to_lsdc_bo(struct ttm_buffer_object *tbo)
{
return container_of(tbo, struct lsdc_bo, tbo);
}
static inline struct lsdc_bo *gem_to_lsdc_bo(struct drm_gem_object *gem)
{
return container_of(gem, struct lsdc_bo, tbo.base);
}
const char *lsdc_mem_type_to_str(uint32_t mem_type);
const char *lsdc_domain_to_str(u32 domain);
struct lsdc_bo *lsdc_bo_create(struct drm_device *ddev,
u32 domain,
size_t size,
bool kernel,
struct sg_table *sg,
struct dma_resv *resv);
struct lsdc_bo *lsdc_bo_create_kernel_pinned(struct drm_device *ddev,
u32 domain,
size_t size);
void lsdc_bo_free_kernel_pinned(struct lsdc_bo *lbo);
int lsdc_bo_reserve(struct lsdc_bo *lbo);
void lsdc_bo_unreserve(struct lsdc_bo *lbo);
int lsdc_bo_pin(struct lsdc_bo *lbo, u32 domain, u64 *gpu_addr);
void lsdc_bo_unpin(struct lsdc_bo *lbo);
void lsdc_bo_ref(struct lsdc_bo *lbo);
void lsdc_bo_unref(struct lsdc_bo *lbo);
u64 lsdc_bo_gpu_offset(struct lsdc_bo *lbo);
size_t lsdc_bo_size(struct lsdc_bo *lbo);
int lsdc_bo_kmap(struct lsdc_bo *lbo);
void lsdc_bo_kunmap(struct lsdc_bo *lbo);
void lsdc_bo_clear(struct lsdc_bo *lbo);
int lsdc_bo_evict_vram(struct drm_device *ddev);
int lsdc_ttm_init(struct lsdc_device *ldev);
void lsdc_ttm_debugfs_init(struct lsdc_device *ldev);
#endif
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment