<<

!1 67

Tizen v2.3 Kernel

Sungkyunkwan University

Embedded Software Lab. @ SKKU !2

Tizen 内核附加特性 49

• 存储管理 – CMA (连续的内存分配器 ), IOMMU, dma_buf, DRM (直 接渲染管理器 ) • ARM Linux没有 DMA_ZONE伙伴 • 有些设备需要物理上的连续的内存 (CMA), 其他设备有 IOMMU从⽽ 能够使⽤不连续的内存 • 对于 D/D⽤户可以共享 DMA存储缓存 – 电源管理 (CPUfreq, Devfreq, PM-QoS, Charger- manager) – KDBUS – V4L2 ( for Linux 2) – EXTCON

Embedded Software Lab. @ SKKU !3

以前参考内核 49

• Linux 3.0.15 – Obsolete LTS (Current: 3.4 & 3.10)

• ⽀持 RD-PQ (Tizen 2) & RD-210 (tizen 1 & 2) – RD-PQ: Exynos4412 – RD-210: Exynos 4210 (Linux 2.6.36 for Tizen 1) • 不好的参照 – 太多的补丁功能 – 太陈旧! 不⽀持 LTS/LTSI – 许多内核⿊客和肮脏的补丁 – git历史删除 • 从⽣产内核中 • 很难阅读

Embedded Software Lab. @ SKKU . LTS? LTSI? !4 67

• Long-Term Stable (LTS) – Maintained by Greg K.H. – Bugfixes for 2 years or longer. – Up to 2 LTS kernels the same time. – Recent: 3.10.39 (2014/5/6) • Long-Term Stable Initiative (LTSI) – Maintained by Greg K.H. and some manufacturers – Forked LTS for Industry. (LTS + Industry Patchset) – Maintain a common Linux base for use in a variety of consumer electronics products – Longer support period. – Recent: 3.10.31-LTSI (2014/2/24)

Embedded Software Lab. @ SKKU !5

Tizen 参考内核的状态 49

• 两个参考内核 : ARM/

• ARM (armv7, aarch64)

– Linux 3.10.y • 3.10.33 @ 2014/05 • 完整的 git历史

– 即将推出 Armv6 support () – 测试 &验证阶段 (集成测试和⽤户空间 )

• Intel (x86, x86_64)

– Linux 3.14.1 – Recent ATOM Soc support merged

Embedded Software Lab. @ SKKU !6

Tizen 内核概述 49

Embedded Software Lab. @ SKKU !7

Tizen 内核概述 (续 ) 49

Embedded Software Lab. @ SKKU Static Memory vs. CMA memory Multimedia !8 example 67

dma_alloc_coherent()

Embedded Software Lab. @ SKKU ION !9 67

• generalized memory manager that Google introduced in the Android 4.0 ICS • Provides way for userland to allocate buffers from various “pools of memory” (aka: heaps) • CMA also allows for large physically contiguous memory allocations by migrating memory to make room for the large allocation – CMA is kernel-internal only for now, and doesn't have a interface to allow userland to allocate buffers or specify constraint options – Migrating pages to make room can cause non-deterministic delays.

Embedded Software Lab. @ SKKU ION !10 67

Embedded Software Lab. @ SKKU !11 67

Tizen v2.3 内存管理

Embedded Software Lab. @ SKKU !12

Tizen 存储管理 49

• 连同图形和多媒体设备 – 图形⾦额多媒体设备 = 有巨⼤缓存的 DMA设备

Embedded Software Lab. @ SKKU !13

DRM是什么 49

• DRM不是数字版权管理

• 直接渲染管理器 – ⽀持直接渲染管理器结构的内核级别图形设备 – 直接渲染 • 应⽤直接与图形设备驱动交互 – 显⽰模式设置 – 图形存储管理

Embedded Software Lab. @ SKKU !14

为什么使⽤ DRM 49

• 可以提供类似于 PC环境的展⽰体验

• 直到现在为什么不使⽤ DRM? – DRM是为计算机设计的 – 嵌⼊式系统 • 性能低 • 内有专⽤图形内存 • 没有⼀个显卡却⼜分开硬件设备 – Linux帧缓冲驱动程序

Embedded Software Lab. @ SKKU !15

为什么使⽤ DRM (续 ) 49

• 改变了嵌⼊式系统 – 强⼤的嵌⼊式 SoCs – 需求 • 显⽰热插拔与克隆拓展模式 • 统⼀内存管理 • 直接渲染 • 不同的设备控制与公共接⼝

Embedded Software Lab. @ SKKU X11 infrastructure !16 67

DIX (Device-Independent X) DDX (Device-Dependent X),

Embedded Software Lab. @ SKKU Early implementation of the Linux graphics stack !17 67

UMS (User-space Mode-Setting) Embedded Software Lab. @ SKKU Early implementation of the Linux graphics stack !18 67 • XFree86 server: the first Linux 2D graphics – Super-user privileges – Access the card directly from , no kernel support – Simple and portable • Utah-GLX: the first hardware-independent 3D accelerated design – User space 3D driver, directly accesses the graphics hardware from user space – 3D hardware was clearly separated from 2D (3Dfx), completely separate driver. • drivers – Another component that could simultaneously access the graphics hardware directly. – To avoid potential conflicts between the framebuffer and XFree86 drivers, – On VT(virtual terminal) switches, the kernel would emit a signal to the X server telling it to save the graphics hardware state. – More fragile and bug-prone drivers. • Drawbacks – Security: unprivileged user space applications be allowed to access the graphics hardware for 3D. – Performance: all GL acceleration had to be indirect through the X protocol

Embedded Software Lab. @ SKKU DRI model !19 67

Embedded Software Lab. @ SKKU DRI/DRM infrastructure !20 67

• To address the reliability and security concerns with the Utah-GLX model • Additional kernel component whose duty is to check the correctness of the 3D command stream, security-wise. • Instead of accessing the card directly, the unprivileged OpenGL application would submit command buffers to the kernel, which would check them for security and then pass them to the hardware for execution. • Trusting user space is no longer required.

• Kernel Mode-Setting (KMS) – Merge the kernel framebuffer functionality into the DRM module – X.Org access the graphics card through the DRM module and run unprivileged. – DRM module is now responsible for providing mode-setting services both as a framebuffer driver and to X.Org.

Mode-setting: configuring video mode to get a picture on the screen. includes choosing the video resolution and refresh rates.

Embedded Software Lab. @ SKKU Framebuffer Drivers !21 67

• Kernel graphics driver exposing its interface through / dev/fb* • Despite their simplicity, framebuffer drivers are still a relevant option for basic 2D display. • Especially for embedded systems – memory footprint is essential – do not require advanced graphics acceleration. • Functionalities – Mode-setting – Optional 2D acceleration • http://www.linux-fbdev.org/HOWTO/index.html

Embedded Software Lab. @ SKKU !22 67 • Put critical initialization of the card in the kernel, for example uploading or setting up DMA areas. • Share the rendering hardware between multiple user space components, and arbitrate access. • Enforce security by preventing applications from performing DMA to arbitrary memory regions, and more generally from programming the card in any way that could result in a security hole. • Manage the memory of the card, by providing video memory allocation functionality to user space. • More recently, the DRM was improved to achieve mode-setting. – Simplifies the previous situation where both the Libdrm interfaces between user space and the DRM DRM and the framebuffer driver were fighting module, unprivileged user space component to access the same GPU. – Instead, the framebuffer driver is removed and framebuffer support is implemented in the DRM.

Embedded Software Lab. @ SKKU DRM of Tizen v2.3 kernel !23 67

• Currently, DRM support is only for Exynos SoCs. (ARM based) – Need Exynos specific DRM driver • Exynos DRM driver – To support graphics hardware of Exynos SoCs – First ARM SoC graphics driver to use the DRM – Merged into the mainline linux 3.2 kernel – linux/drivers/gpu/drm/exynos

Embedded Software Lab. @ SKKU !24

图形 (DRM / GEM) 49

• 图形 : – DRM (直接渲染管理 ) / GEM (图形处理管理 )

GEM •由 Intel开发的框架 • 为了管理图形存储 •缓存管理框架 • 分配和共享

Embedded Software Lab. @ SKKU !25

DRM / GEM 分配 49

GEM 分配步骤 @ Tizen (Generic)

•DRM_IOCTL_MODE_CREATE_DUMB – 创建 GEM全局 & user GEM handle(per ) • dumb_create() of struct drm_driver – 没有物理内存分配 •DRM_IOCTL_MODE_MAP_DUMB – 创建虚假 mmap offset of a gem object 并将这个对象传递给⽤户 • A hash key of the gem object. • dumb_map_offset() of struct drm_driver •MMAP – 请求 mmap基于哈希键作为偏移量 – 创建⽤户地址空间 – 设置缓存属性 – 还没有映射到物理内存

Embedded Software Lab. @ SKKU !26

DRM / GEM 分配 (续 ) 49

GEM 分配步骤 @ Tizen (Generic)

• 按需分⻚ – 实现和注册⼀个错误处理程序 • With a page fault, allocate a page and map the page. – vma->vm_ops->fault = xxx_drm_gem_fault

•使⽤ !

•DRM_IOCTL_MODE_DESTROY_DUMB – 移除 GEM handle & object – 释放内存 – 实现 dumb_destroy() of struct drm_driver

Embedded Software Lab. @ SKKU !27

DRM / GEM 分配 (续 ) 49

GEM 分配步骤 @ Tizen (Exynos Only)

•DRM_IOCTL_EXYNOS_GEM_CREATE – 只使⽤⽤户需要的⼤⼩和缓存类型 – 创建 gem全局对象和 user gem handle(per process) – 分配物理内存 •DRM_IOCTL_EXYNOS_GEM_MMAP – 创建⽤户地址空间 – 将⽤户地址空间映射到物理内存

•LIBDRM of Exynos uses these , not the generic.

Embedded Software Lab. @ SKKU !28

DRM / GEM 共享 49

GEM 共享 @ Tizen

•DRM_IOCTL_GEM_FLINK – “I will share this GEM to others.” – Create GEM object name for the given GEM handle • 共享全局键值

•DRM_IOCTL_GEM_OPEN – “I want to use the shared GEM.” – 基于已有 GEM对象名称,创建 Create GEM handle •DRM_IOCTL_GEM_CLOSE

Embedded Software Lab. @ SKKU !29

Tizen Kernel MM –Multimedia (V4L2 / VB2) 49

Embedded Software Lab. @ SKKU !30

V4L2 / VB2 49

• 对于多媒体设备, Tizen推荐在内核中使⽤

– Video input (codec & camera) & Radio

– 然⽽,尽管内核具有 ..: • Gstreamer/OpenMAX plugins • A method to share with other F/W via DMABUF of UMM • 如果使⽤ V4L2/VB2, 事情会变得更简单

Embedded Software Lab. @ SKKU !31

Tizen Kernel MM –OpenGL / G3D-GPU 49

Embedded Software Lab. @ SKKU !32

OpenGL / G3D-GPU 49

• ⼤部分 ARM SoC GPUs (MALI, SGX, …)使⽤ 它们⾃⼰的内存 – 例如 , Exynos4412/4210 Tizen Reference Kernel 有 Mali w/ UMM. • Mali DDK modified to be compatible with UMM-DMABUF. • 如果 GPU驱动使⽤ DRM, 将会更好 . – (and make them GPL)

Embedded Software Lab. @ SKKU !33 图形化和显⽰器使⽤场景 49

Embedded Software Lab. @ SKKU !34 图形化和显⽰器使⽤场景 49

Embedded Software Lab. @ SKKU !35 图形化和显⽰器使⽤场景 49

Embedded Software Lab. @ SKKU !36 图形化和显⽰器使⽤场景 49

Embedded Software Lab. @ SKKU !37 图形化和显⽰器使⽤场景 49

Embedded Software Lab. @ SKKU !38 Tizen 内核缓冲共享 49

Embedded Software Lab. @ SKKU !39 Tizen 内核缓冲共享 49

• Tizen平台和硬件的需求 – 不同的内存管理器 : GEM, VB2, GPU-adhoc,… – 共享缓冲 • w/o memcpy • From and to users • 不要直接暴露给⽤户 (e.g., 物理地址 ) ➔UMM DMABUF!

Embedded Software Lab. @ SKKU !40 统⼀的内存管理器 (UMM) 49

• 由 Jesse Barker引⼊ , 2011 • 包含 – DMABUF (共享缓冲 ) • Export – GEM/VB2/… object ! DMABUF • Import – DMABUF ! GEM/VB2/… object • ⽤户空间将 DMABUF当作⽂件描述器 – DMA mapping API for allocation – CMA (Contiguous Memory Allocator) – IOMMU (MMU for I/O devs)

Embedded Software Lab. @ SKKU !41 DMA缓冲共享 : Camera ! Display 49

Embedded Software Lab. @ SKKU !42 DMA 缓冲共享⽰例 49

• Camera App 1)请求 V4L2 相机缓存 (U) 2)分配 CMA 缓存 (K) 3)向 V4L2缓存请求⼀个 camera 帧 (U) 4存储 camera帧 & 通知⽤户 (K) 5)请求 DMABUF 为 V4L2 camera 缓冲导出 (U) 6)dma_buf_exporter() (K) • Create DMABUF from V4L2 buffer 7)dma_buf_fd() (K) • Provide FD of the DMABUF to user

Embedded Software Lab. @ SKKU !43 DMA缓冲共享⽰例 49

• Camera App 1)请求 V4L2 camera 缓冲 (U) 2)分配 CMA 缓冲 (K) 3)向 V4L2缓存请求⼀个 camera 帧 (U) 4)存储 camera帧 & 通知⽤户 (K) 5)请求 DMABUF 为 V4L2 camera 缓冲导出 (U) 6)dma_buf_exporter() (K) • Create DMABUF from V4L2 buffer 7)dma_buf_fd() (K) • Provide FD of the DMABUF to user

Embedded Software Lab. @ SKKU !44 DMA缓冲共享⽰例 49

• Camera App 1)请求 V4L2 camera 缓冲 (U) 2)分配 CMA 缓冲 (K) 3)向 V4L2缓存请求⼀个 camera 帧 (U) 4)存储 camera帧 & 通知⽤户 (K) 5)请求 DMABUF 为 V4L2 camera 缓冲导出 (U) 6)dma_buf_exporter() (K) • Create DMABUF from V4L2 buffer 7)dma_buf_fd() (K) • Provide FD of the DMABUF to user

Embedded Software Lab. @ SKKU !45 DMA缓冲共享⽰例 49

• Camera App 8)请求 FD->GEM 对话 (U) 9)dma_buf_get(fd) (K) • Get DMABUF from FD 10)dma_buf_attach(dma-buf) / dma_buf_map_attachment() (K) • Get Buffer from DMABUF 11)Import as GEM, send user (K) 12)Request GEM object name (U) 13)Return GEM object name (K) 14)Send GEM object name to X (U)

Embedded Software Lab. @ SKKU !46 DMA缓冲共享⽰例 49

• Camera App 8)Request FD->GEM conversion (U) 9)dma_buf_get(fd) (K) • Get DMABUF from FD 10)dma_buf_attach(dma-buf) / dma_buf_map_attachment() (K) • Get Buffer from DMABUF 11)Import as GEM, send user (K) 12)Request GEM object name (U) 13)Return GEM object name (K) 14)Send GEM object name to X (U)

Embedded Software Lab. @ SKKU !47 DMA缓冲共享⽰例 49

• X server 15)Convert given GEM object name to GEM. Display its content. (U & K)

Embedded Software Lab. @ SKKU !48 DMA缓冲共享⽰例 49

• 使⽤后关闭 1)释放 GEM对象 (U) 2)删除来在 DMABUF的引⽤ (K) 3)关闭 cam app 的 (DMABUF-FD)(U) 4)(没有 DMABUF的其他引⽤ ) 释放执 ⾏完毕的回调函数 (K)

Embedded Software Lab. @ SKKU !49 缓冲同步 49

Embedded Software Lab. @ SKKU !50 问题定义 49

• 简单的使⽤场景 – 1. CPU写⼊ buffer1. – 2. CPU告诉 GPU要在 buffer1执⾏什么任务 . – 3. GPU在 buffer1执⾏任务 . • GPU 完成 . – 4. CPU读 buffer1

How to ensure CPU won’t use buffer 1 until step 4?

CPU GPU

1 2 BUS

4 3 DRAM

Embedded Software Lab. @ SKKU !51 问题定义 49

• 如果有两个线程使⽤ GPU? – 如果这两个线程读同⼀块 buffer? • 甚⾄没有意识到这⼀点 ? • 如果这两个 DMA设备( GPU, FIMC)共享 buffer? – 但是很难在驱动器或⽤户线程知道 . – FIMC从来不知道 GPU什么时候完成 – FIMC 从来不知道 Threads1什么时候停⽌使⽤ “buffer1” – Threads2从来不知道 GPU什么时候停⽌使⽤ “buffer1”

Embedded Software Lab. @ SKKU !52 缓冲同步 49

• TGL (Tizen Global Lock) @ Tizen 2.0 – … 让⽤户空间解决这个问题 … – 需要内核补丁 . • Framework (Google) – Jun, 2012. Resources w/o DMABUF (similar with TGL) • KDS (Kernel Dependency System, ARM) – May 2012 / DMABUF-compatible • DMA Fence Framework (Canonical/TI) – Aug 2012 / DMABUF-compatible – Work-In-Progress

Embedded Software Lab. @ SKKU !53

49

K-dbus

Embedded Software Lab. @ SKKU !54 Kdbus –D-bus 是什么 ? 49

• D-Bus – 消息总线系统 – ⽅法调⽤事务 – 信号 – ⼲播

Embedded Software Lab. @ SKKU !55 Kdbus –K-dbus 是什么 ? 49

• Low-level, native kernel D-Bus transport – 所有进程间的交流都发⽣在 /dev/kdbus⾥的特殊的设备节点上 • Receiver buffers – 单个拷⻉给⺫标 • memfds – 内存区域的⽂件描述符 – 零拷⻉实现

Embedded Software Lab. @ SKKU !56 D-bus vs Kdbus 49

Embedded Software Lab. @ SKKU !57 67

Others

Embedded Software Lab. @ SKKU !58 EXTCON (外部连接器 ) 49

• Yet Another Weird Ad-hoc Kernel Hacks Removed • 管理 cable & ports的状态 – A port with multiple cables (docks, multi-cables, …) – A port with multiple modes (USB, HDMI, TA, …) • 3.5pi: stereo, stereo+mic, stereo+mic+buttons, stereo+buttons, mono, … • 与 Switch兼容 – Android Switch drivers can be easily ported • Refer to Linux/Documentation (porting guide for switch driver) – Extcon drivers export both Switch and Extcon interfaces (compat mode) • In Reference Device – MUIC (USB+HDMI+TA+DOCK+…) – 3.5Pi Jack

Embedded Software Lab. @ SKKU !59 Charger 49

• Charger 管理器 (/drivers/power/charger- manager.) – All needed by Tizen user space are prepared – No OAL modification required • OR 提供 battery/charger 接⼝通过 power-supply- class • 使⽤ EXTCON 做 Cable-Input (MUIC in mobile) – 类的切换在 Linux系统中不再可⽤ • Note: ⼀些 SOC (state-of-charge) 值在 mobile ⽂ 件中被需要 . 除⾮ , Tizen将假定 SOC是 0 !

Embedded Software Lab. @ SKKU Power Management (Tizen 2.x) !60 67

• Tizen v2.3 Kernel does not use opportunistic sleep like Android wake-lock • Userspace(Power Manager) should request sleep directly

Embedded Software Lab. @ SKKU Power Management (Tizen 3.0) !61 67

• Components (e.g., OOM) are merged and consolidated into Resourced.

• Tizen v3.0 use deviced ➢Device control access is merged into deviced

• Tizen v2.x use Power Manager ➢App can access kernel device node by using devman

Embedded Software Lab. @ SKKU !62 Power Management 49

• Tizen3.0以及之后的版本推荐: – 不要使⽤ DVFS (CPUfreq/Devfreq) 最⼩ /最⼤ ABIs • PASS (Power-Aware System Service in Deviced) uses – 使⽤ (keep their standard ABIs for PASS) • CPUfreq& Devfreq(DVFS for non CPU devices if you have them) • Thermal FW – PASS给 DVFS/Hotplug提⽰ • 基于⽤户空间的信息 . • 基于其他内核 ABIs,如终端 • ⾼度可配置化 . (控制 performance & power)

Embedded Software Lab. @ SKKU Other Peripherals: Sound !63 67 • Use ALSA Scenario (www.alsa-project.org) – Make hardware plugin and based on ALSA standard. • https://wiki.tizen.org/wiki/Porting_Guide#Audio

Embedded Software Lab. @ SKKU Other Peripherals: BlueTooth !64 67

• Use Bluez(www.bluez.org) • https://wiki.tizen.org/wiki/Porting_Guide#Bluetooth

Embedded Software Lab. @ SKKU Other Peripherals: WiFi !65 67

• Use wpa-supplicant • https://wiki.tizen.org/wiki/Porting_Guide#WLAN

Embedded Software Lab. @ SKKU Core Daemons (Tizen 3.0) !66 67 • ( & management) • Deviced/Resourced – Power-manager, devman, and system-server are consolidated. • DBUS: The usage of Vconf is reduced

Embedded Software Lab. @ SKKU OEM Adaptation Layer (OAL) !67 67 • https://wiki.tizen.org/wiki/Porting_Guide#System_OAL • Tizen 2.0/adaptation/* – Related to System FW: • Tizen 2.0/adaptation/ap_samsung/device-manager-plugin-exynos • Tizen 2.0/adaptation/intel_mfld/device-manager-plugin-mfld-blackbay – Related to Alsa (sound) : • Tizen 2.0/adaptation/devices/alsa-scenario-*

Embedded Software Lab. @ SKKU