xiaoxiang781216 commented on code in PR #12041:
URL: https://github.com/apache/nuttx/pull/12041#discussion_r1579481974


##########
include/nuttx/mm/map.h:
##########
@@ -193,6 +193,45 @@ void vm_release_region(FAR struct mm_map_s *mm, FAR void 
*vaddr,
 
 #endif
 
+#ifdef CONFIG_ARCH_VMA_MAPPING
+
+/****************************************************************************
+ * Name: vm_map_region
+ *
+ * Description:
+ *   Allocate virtual memory and maps given physical memory into user space
+ *   of the current process.
+ *
+ * Input Parameters:
+ *   paddr - Starting physical address
+ *   size  - Size of the address range
+ *
+ * Returned Value:
+ *   Virtual address if success, or NULL if error
+ *
+ ****************************************************************************/
+
+FAR char *vm_map_region(uintptr_t paddr, size_t size);

Review Comment:
   ```suggestion
   FAR void *vm_map_region(uintptr_t paddr, size_t size);
   ```



##########
include/nuttx/mm/map.h:
##########
@@ -193,6 +193,45 @@ void vm_release_region(FAR struct mm_map_s *mm, FAR void 
*vaddr,
 
 #endif
 
+#ifdef CONFIG_ARCH_VMA_MAPPING
+
+/****************************************************************************
+ * Name: vm_map_region
+ *
+ * Description:
+ *   Allocate virtual memory and maps given physical memory into user space
+ *   of the current process.
+ *
+ * Input Parameters:
+ *   paddr - Starting physical address
+ *   size  - Size of the address range
+ *
+ * Returned Value:
+ *   Virtual address if success, or NULL if error
+ *
+ ****************************************************************************/
+
+FAR char *vm_map_region(uintptr_t paddr, size_t size);
+
+/****************************************************************************
+ * Name: vm_unmap_region
+ *
+ * Description:
+ *   Unmap previously mapped userspace device and release the virtual memory.
+ *
+ * Input Parameters:
+ *   vaddr - Starting virtual address of the mapped device
+ *   size - Size of the address range
+ *
+ * Returned Value:
+ *   OK for success or negative value for error
+ *
+ ****************************************************************************/
+
+int vm_unmap_region(FAR char *vaddr, size_t size);

Review Comment:
   ```suggestion
   int vm_unmap_region(FAR void *vaddr, size_t size);
   ```



##########
mm/map/vm_map.c:
##########
@@ -0,0 +1,97 @@
+/****************************************************************************
+ * mm/map/vm_map.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <debug.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/mm/map.h>
+#include <nuttx/pgalloc.h>
+#include <nuttx/sched.h>
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+#ifdef CONFIG_ARCH_VMA_MAPPING
+
+/* map physical region to userspace */
+
+FAR char *vm_map_region(uintptr_t paddr, size_t size)
+{
+  FAR char *vaddr;
+  size_t    npages  = MM_NPAGES(size);
+  int       ret     = OK;
+  uint      i       = 0;
+  uintptr_t tvaddr;
+  uintptr_t tpaddr;
+
+  DEBUGASSERT(npages > 0);
+  DEBUGASSERT(MM_ISALIGNED(paddr));
+
+  vaddr = vm_alloc_region(get_current_mm(), 0, size);
+  if (vaddr)
+    {
+      tvaddr    = (uintptr_t)vaddr;

Review Comment:
   ```suggestion
         tvaddr = (uintptr_t)vaddr;
   ```



##########
mm/map/vm_map.c:
##########
@@ -0,0 +1,97 @@
+/****************************************************************************
+ * mm/map/vm_map.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <debug.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/mm/map.h>
+#include <nuttx/pgalloc.h>
+#include <nuttx/sched.h>
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+#ifdef CONFIG_ARCH_VMA_MAPPING
+
+/* map physical region to userspace */
+
+FAR char *vm_map_region(uintptr_t paddr, size_t size)
+{
+  FAR char *vaddr;

Review Comment:
   ```suggestion
     FAR void *vaddr;
   ```



##########
mm/map/vm_map.c:
##########
@@ -0,0 +1,97 @@
+/****************************************************************************
+ * mm/map/vm_map.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <debug.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/mm/map.h>
+#include <nuttx/pgalloc.h>
+#include <nuttx/sched.h>
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+#ifdef CONFIG_ARCH_VMA_MAPPING
+
+/* map physical region to userspace */
+
+FAR char *vm_map_region(uintptr_t paddr, size_t size)
+{
+  FAR char *vaddr;
+  size_t    npages  = MM_NPAGES(size);
+  int       ret     = OK;
+  uint      i       = 0;
+  uintptr_t tvaddr;
+  uintptr_t tpaddr;
+
+  DEBUGASSERT(npages > 0);
+  DEBUGASSERT(MM_ISALIGNED(paddr));
+
+  vaddr = vm_alloc_region(get_current_mm(), 0, size);
+  if (vaddr)
+    {
+      tvaddr    = (uintptr_t)vaddr;
+      tpaddr    = (uintptr_t)paddr;
+      for (; i < npages; i++, tvaddr += MM_PGSIZE, tpaddr += MM_PGSIZE)
+        {
+          ret = up_shmat(&tpaddr, 1, tvaddr);
+          if (ret)
+            {
+              goto errorout;
+            }
+        }
+    }
+
+  return vaddr;
+
+errorout:
+  if (vaddr)
+    {
+      if (i)   /* undo mapped pages */
+        {
+          up_shmdt((uintptr_t)vaddr, i);
+        }
+
+      vm_release_region(get_current_mm(), (FAR void *)vaddr, size);

Review Comment:
   ```suggestion
         vm_release_region(get_current_mm(), vaddr, size);
   ```



##########
mm/map/vm_map.c:
##########
@@ -0,0 +1,97 @@
+/****************************************************************************
+ * mm/map/vm_map.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <debug.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/mm/map.h>
+#include <nuttx/pgalloc.h>
+#include <nuttx/sched.h>
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+#ifdef CONFIG_ARCH_VMA_MAPPING
+
+/* map physical region to userspace */
+
+FAR char *vm_map_region(uintptr_t paddr, size_t size)
+{
+  FAR char *vaddr;
+  size_t    npages  = MM_NPAGES(size);

Review Comment:
   ```suggestion
     size_t    npages = MM_NPAGES(size);
   ```



##########
mm/map/vm_map.c:
##########
@@ -0,0 +1,97 @@
+/****************************************************************************
+ * mm/map/vm_map.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <debug.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/mm/map.h>
+#include <nuttx/pgalloc.h>
+#include <nuttx/sched.h>
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+#ifdef CONFIG_ARCH_VMA_MAPPING
+
+/* map physical region to userspace */
+
+FAR char *vm_map_region(uintptr_t paddr, size_t size)
+{
+  FAR char *vaddr;
+  size_t    npages  = MM_NPAGES(size);
+  int       ret     = OK;
+  uint      i       = 0;
+  uintptr_t tvaddr;
+  uintptr_t tpaddr;
+
+  DEBUGASSERT(npages > 0);
+  DEBUGASSERT(MM_ISALIGNED(paddr));
+
+  vaddr = vm_alloc_region(get_current_mm(), 0, size);
+  if (vaddr)
+    {
+      tvaddr    = (uintptr_t)vaddr;
+      tpaddr    = (uintptr_t)paddr;
+      for (; i < npages; i++, tvaddr += MM_PGSIZE, tpaddr += MM_PGSIZE)
+        {
+          ret = up_shmat(&tpaddr, 1, tvaddr);
+          if (ret)
+            {
+              goto errorout;
+            }
+        }
+    }
+
+  return vaddr;
+
+errorout:
+  if (vaddr)
+    {
+      if (i)   /* undo mapped pages */
+        {
+          up_shmdt((uintptr_t)vaddr, i);
+        }
+
+      vm_release_region(get_current_mm(), (FAR void *)vaddr, size);
+    }
+
+  return 0;
+}
+
+/* unmap userspace device pointer */
+
+int vm_unmap_region(FAR char *vaddr, size_t size)
+{
+  size_t npages = MM_NPAGES(size);
+  int ret;
+
+  DEBUGASSERT(MM_ISALIGNED(vaddr));
+  DEBUGASSERT(npages);
+  ret = up_shmdt((uintptr_t)vaddr, npages);
+  vm_release_region(get_current_mm(), (FAR void *)vaddr, size);

Review Comment:
   ```suggestion
     vm_release_region(get_current_mm(), vaddr, size);
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to