hyperion.ng
IonBuffer.h
1 /*
2  *
3  * Copyright (C) 2016 OtherCrashOverride@users.noreply.github.com.
4  * All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2, as
8  * published by the Free Software Foundation.
9  *
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
13  * more details.
14  *
15 */
16 #pragma once
17 
18 #include <sys/ioctl.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <fcntl.h>
22 #include <sys/mman.h>
23 #include <stdexcept>
24 
25 #include "ion.h"
26 #include "meson_ion.h"
27 
28 
29 class IonBuffer
30 {
31  size_t bufferSize = 0;
32  ion_user_handle_t handle = 0;
33  int exportHandle = 0;
34  size_t length = 0;
35  unsigned long physicalAddress = 0;
36 
37 
38  static int ion_fd; // = -1;
39 
40 
41 public:
42 
43  size_t BufferSize() const
44  {
45  return bufferSize;
46  }
47 
48  ion_user_handle_t Handle() const
49  {
50  return handle;
51  }
52 
53  int ExportHandle() const
54  {
55  return exportHandle;
56  }
57 
58  size_t Length() const
59  {
60  return length;
61  }
62 
63  unsigned long PhysicalAddress() const
64  {
65  return physicalAddress;
66  }
67 
68 
69 
70  IonBuffer(size_t bufferSize)
71  : bufferSize(bufferSize)
72  {
73  if (bufferSize < 1)
74  throw std::runtime_error("bufferSize < 1");
75 
76 
77  if (ion_fd < 0)
78  {
79  ion_fd = open("/dev/ion", O_RDWR);
80  if (ion_fd < 0)
81  {
82  throw std::runtime_error("open ion failed.");
83  }
84  }
85 
86 
87  int io;
88 
89  // Allocate a buffer
90  ion_allocation_data allocation_data = { 0 };
91  allocation_data.len = bufferSize;
92  allocation_data.heap_id_mask = ION_HEAP_CARVEOUT_MASK;
93 
94 #if defined(__aarch64__)
95  allocation_data.flags = ION_FLAG_CACHED_NEEDS_SYNC; //ION_FLAG_CACHED;
96 #else
97  allocation_data.flags = 0;
98 #endif
99 
100  io = ioctl(ion_fd, ION_IOC_ALLOC, &allocation_data);
101  if (io != 0)
102  {
103  throw std::runtime_error("ION_IOC_ALLOC failed.");
104  }
105 
106 // fprintf(stderr, "ion handle=%d\n", allocation_data.handle);
107 
108 
109  // Map/share the buffer
110  ion_fd_data ionData = { 0 };
111  ionData.handle = allocation_data.handle;
112 
113  io = ioctl(ion_fd, ION_IOC_SHARE, &ionData);
114  if (io != 0)
115  {
116  throw std::runtime_error("ION_IOC_SHARE failed.");
117  }
118 
119 // fprintf(stderr, "ion map=%d\n", ionData.fd);
120 
121 
122  // Get the physical address for the buffer
123  meson_phys_data physData = { 0 };
124  physData.handle = ionData.fd;
125 
126  ion_custom_data ionCustomData = { 0 };
127  ionCustomData.cmd = ION_IOC_MESON_PHYS_ADDR;
128  ionCustomData.arg = (long unsigned int)&physData;
129 
130  io = ioctl(ion_fd, ION_IOC_CUSTOM, &ionCustomData);
131  if (io != 0)
132  {
133  throw std::runtime_error("ION_IOC_CUSTOM failed.");
134  }
135 
136  // Assignment
137  handle = allocation_data.handle;
138  exportHandle = ionData.fd;
139  length = allocation_data.len;
140  physicalAddress = physData.phys_addr;
141 
142 // fprintf(stderr, "ion phys_addr=%lu\n", physicalAddress);
143  }
144 
145  virtual ~IonBuffer()
146  {
147  ion_handle_data ionHandleData = { 0 };
148  ionHandleData.handle = handle;
149 
150  int io = ioctl(ion_fd, ION_IOC_FREE, &ionHandleData);
151  if (io != 0)
152  {
153  throw std::runtime_error("ION_IOC_FREE failed.");
154  }
155  }
156 
157 
158  void Sync()
159  {
160 
161 #if defined(__aarch64__)
162  ion_fd_data ionFdData = { 0 };
163  ionFdData.fd = ExportHandle();
164 
165  int io = ioctl(ion_fd, ION_IOC_SYNC, &ionFdData);
166  if (io != 0)
167  {
168  throw std::runtime_error("ION_IOC_SYNC failed.");
169  }
170 #endif
171 
172  }
173 
174  void* Map()
175  {
176  void* result = mmap(NULL,
177  Length(),
178  PROT_READ | PROT_WRITE,
179  MAP_FILE | MAP_SHARED,
180  ExportHandle(),
181  0);
182  if (result == MAP_FAILED)
183  {
184  throw std::runtime_error("mmap failed.");
185  }
186 
187  return result;
188  }
189 };
struct ion_fd_data - metadata passed to/from userspace for a handle/fd pair : a handle : a file descr...
Definition: ion.h:104
struct ion_custom_data - metadata passed to/from userspace for a custom ioctl : the custom ioctl func...
Definition: ion.h:125
DOC: Ion Userspace API.
Definition: ion.h:86
Definition: IonBuffer.h:29
struct ion_handle_data - a handle passed to/from the kernel : a handle
Definition: ion.h:113
Definition: meson_ion.h:29